【问题标题】:The annotation Propagation.NEVER doesn't work注释 Propagation.NEVER 不起作用
【发布时间】:2015-09-14 08:06:32
【问题描述】:

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

为什么它不起作用?

谢谢:)

【问题讨论】:

  • 如果您可以添加异常堆栈跟踪并列出您已经尝试过的解决方案会很棒
  • @VirajNalawade,看看控制台会发生什么

标签: java spring hibernate hibernate-annotations propagation


【解决方案1】:

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

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

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

【讨论】:

  • 谢谢!您的选择确实有效。我仍然阅读文档,解决我的问题的另一种方法如下: 结果我收到以下堆栈跟踪:线程“main”中的 xception org.hibernate.HibernateException : 无法获取当前线程的事务同步会话。也许你会提示,我做错了什么?
  • 不知道。我从不使用那种模式。
猜你喜欢
  • 1970-01-01
  • 2014-08-14
  • 1970-01-01
  • 2013-09-20
  • 2020-10-09
  • 2012-09-19
  • 2017-08-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多