java - 注释 Propagation.NEVER 不起作用

标签 java spring hibernate hibernate-annotations propagation

程序员!我不明白 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

为什么不起作用?

谢谢:)

最佳答案

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

如果您从单个 bean A 的 method1 调用 method2,您的调用将不再被事务代理拦截,因此 Spring 完全不知道 method2() 已被调用。因此没有任何东西可以检查是否存在交易。

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

关于java - 注释 Propagation.NEVER 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31088938/

相关文章:

java - 无法从解析中的安装表中检索数据

java - 使用canvas向JFrame添加其他组件

java - Hibernate中连续命名参数问题(Spring HibernateTemplate)

java - 如何利用 hibernate c3p0连接池?

java - 如何在 Hibernate - Criteria Queries 中获得大于记录?

java - EntityManager 持久化和合并在 @Transactional 方法中不起作用

java - EDI 到 XML 转换

java - 自定义布局中 switchpreference 中开/关切换的颜色变化

java - 使用 nginx tomcat 进行反向代理?

java - 具有单个参数的 Spring Data JPA "OR"