休眠传播永远不会起作用

时间:2015-06-27 12:47:45

标签: java spring hibernate hibernate-annotations propagation

程序员!我无法理解如何正确归因work -propagation(@Transactional)。请帮忙)

<tx:annotation-driven transaction-manager="transactionManager"/>

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

<bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="packagesToScan" value="com.springapp.mvc"></property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.jdbc.batch_size">10</prop>
                <prop key="hibernate.max_fetch_depth">3</prop>
                <prop key="jdbc.fetch_size">50</prop>
                <prop key="hbm2ddl.auto">create-drop</prop>
            </props>
        </property>
        <property name = "dataSource" ref = "dataSource"></property>
    </bean>

服务类

   @Service
   public class BookManager implements IBookManager {

      @Autowired
      private  SessionFactory  sessionFactory;

      @Override
      @Transactional(propagation = Propagation.REQUIRES_NEW)
      public void method1() {
          Session session = sessionFactory.getCurrentSession();
          Book book = (Book) session.load(Book.class, 1);
          System.out.println("first: " + book.getTitle());
         method2();
      }

      @Override
      @Transactional(propagation = Propagation.NEVER)
      public void method2() {
            System.out.println("hello");
     }
 }

我希望方法method2抛出一个异常,就像设置属性Propagation.NEVER一样。但这种情况并没有发生。

输出

 июн 27, 2015 3:35:35 PM org.hibernate.dialect.Dialect <init>
 INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
 июн 27, 2015 3:35:35 PM  org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
 INFO: HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
 июн 27, 2015 3:35:36 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
 INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
 июн 27, 2015 3:35:36 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
 INFO: HHH000397: Using ASTQueryTranslatorFactory
 июн 27, 2015 3:35:36 PM org.springframework.orm.hibernate4.HibernateTransactionManager           afterPropertiesSet
 INFO: Using DataSource [org.apache.commons.dbcp.BasicDataSource@6c5945a7] of Hibernate SessionFactory for HibernateTransactionManager
 Hibernate: select book0_.id as id1_0_0_, book0_.title as title2_0_0_ from books book0_ where book0_.id=?
 first: 33
 hello

 Process finished with exit code 0

为什么呢?感谢:)

1 个答案:

答案 0 :(得分:2)

Spring中的事务是基于代理的:当bean A调用事务bean B时,它实际上调用动态代理的方法,该方法处理事务的开放,然后委托给实际的bean B,然后处理提交/回滚事务。

如果从单个bean A的method1调用method2,则不再由事务代理拦截您的调用,因此Spring完全不知道已调用method2()。所以没有什么可以检查没有交易。

将method2放在另一个 bean中,在BookManager中注入,一切都将按预期工作。