Spring Framework
  1. Spring Framework
  2. SPR-5082

@Transactional annotation doesn't work for scanned component, again

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 2.5 final
    • Fix Version/s: None
    • Component/s: Transaction
    • Labels:
      None
    • Last commented by a User:
      true

      Description

      Please see the following example:

      @Service
      @Transactional
      public class AccountService {

      @Autowired
      private AccountDao accountDao;

      public void updateLoginInfo(Account account)

      { accountDao.updateLoginInfo(account); someOtherStatementsMightThrowException(); }

      }

      And everything supports the annotation is configurated:

      <context:annotation-config />
      <context:component-scan base-package="com.xyz" />
      <tx:annotation-driven transaction-manager="transactionManager" />

      We use @Service annotation on service bean, that is, it could be autowired to any bean depends on it.

      But the @Transactional annotation doesn't work, until I remove @Service annotation and explicit define the bean in context configuration xml:

      <bean id="accountService" class="com.xyz.service.AccountService" />

      I'm wondering if there is any step I missed, or it supposes to work like that? Thanks.

        Issue Links

          Activity

          Hide
          Yuwei Zhao added a comment -

          Reply to Julien Dubois:

          > You just messed up your component-scanning: your "Service" bean is in
          > fact declared both in your applicationContext.xml file, and your servlet.xml
          > file.

          I have include-filter specified in my examples,

          In applicationContext.xml:

          <context:component-scan base-package="com.xyz">
          <context:include-filter type="regex" expression=".*DAOImpl" />
          <context:include-filter type="regex" expression=".*ServiceImpl" />
          </context:component-scan>

          In servlet.xml:

          <context:component-scan base-package="com.xyz">
          <context:include-filter type="regex" expression=".*Controller"/>
          </context:component-scan>

          You use different packages in your proposed solution and I use regular expression to select targeting beans. By principle there is no difference. The "Service" bean is not declared twice.

          Show
          Yuwei Zhao added a comment - Reply to Julien Dubois: > You just messed up your component-scanning: your "Service" bean is in > fact declared both in your applicationContext.xml file, and your servlet.xml > file. I have include-filter specified in my examples, In applicationContext.xml: <context:component-scan base-package="com.xyz"> <context:include-filter type="regex" expression=".*DAOImpl" /> <context:include-filter type="regex" expression=".*ServiceImpl" /> </context:component-scan> In servlet.xml: <context:component-scan base-package="com.xyz"> <context:include-filter type="regex" expression=".*Controller"/> </context:component-scan> You use different packages in your proposed solution and I use regular expression to select targeting beans. By principle there is no difference. The "Service" bean is not declared twice.
          Hide
          Rohit Kanchan added a comment -

          @Yuwei Zhao.. r u able to resolve this? I am facing the same issue with Spring 3.1. I have tried different things explained here, nothing worked for me. I have removed @Service from service layer and added into the servlet xml file, still I am getting following exception. Please let me know if any one of u got solution of this. Thanks in advance.

          javax.persistence.TransactionRequiredException: no transaction is in progress
          at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManag
          erImpl.java:978)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
          java:39)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
          sorImpl.java:25)
          at java.lang.reflect.Method.invoke(Method.java:597)
          at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEnti

          Show
          Rohit Kanchan added a comment - @Yuwei Zhao.. r u able to resolve this? I am facing the same issue with Spring 3.1. I have tried different things explained here, nothing worked for me. I have removed @Service from service layer and added into the servlet xml file, still I am getting following exception. Please let me know if any one of u got solution of this. Thanks in advance. javax.persistence.TransactionRequiredException: no transaction is in progress at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManag erImpl.java:978) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl. java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces sorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEnti
          Hide
          Yuwei Zhao added a comment -

          @Rohit Kanchan
          By the book, service components shall be put into applicationContext.xml, not servlet.xml, unless you only have one IoC Container.

          In my experience, if @Service annotation is removed, and <bean id="foo" class="...." /> is put into applicationContext.xml, it works fine.

          Show
          Yuwei Zhao added a comment - @Rohit Kanchan By the book, service components shall be put into applicationContext.xml, not servlet.xml, unless you only have one IoC Container. In my experience, if @Service annotation is removed, and <bean id="foo" class="...." /> is put into applicationContext.xml, it works fine.
          Hide
          Gleb Schukin added a comment -

          I had the same issue. Adding mode="proxy" to tx:annotation-driven fixed it.

          Show
          Gleb Schukin added a comment - I had the same issue. Adding mode="proxy" to tx:annotation-driven fixed it.
          Hide
          Julien Dubois added a comment -

          Yuwei : read my comment again. This is not how include-filter work (have a look at use-default-filters="false").

          This is definitely not a bug, just a configuration error.

          But I agree this configuration is complex, I often get questions about this, and this is a huge source of mistakes.

          My recommandation : use the simplest configuration possible, as I have in my comment, unless you really need something special.

          Show
          Julien Dubois added a comment - Yuwei : read my comment again. This is not how include-filter work (have a look at use-default-filters="false"). This is definitely not a bug, just a configuration error. But I agree this configuration is complex, I often get questions about this, and this is a huge source of mistakes. My recommandation : use the simplest configuration possible, as I have in my comment, unless you really need something special.

            People

            • Assignee:
              Juergen Hoeller
              Reporter:
              Yuwei Zhao
              Last updater:
              Julien Dubois
            • Votes:
              10 Vote for this issue
              Watchers:
              16 Start watching this issue

              Dates

              • Created:
                Updated:
                Days since last comment:
                1 year, 8 weeks, 1 day ago