How to use Atomikos Transaction Essentials with Hibernate >= 4.3

前端 未结 5 483
悲&欢浪女
悲&欢浪女 2020-12-09 05:54

I switched from Hibernate 4.2 to Hibernate 4.3 and my project is not working any more. I\'m getting an

HibernateException: Unable to locate current JT

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-09 06:26

    The following is an alternative approach that works with Spring configuration. This is different from Anton's approach in that is does not rely on instance method writes to a static field (which is generally considered bad practice).

    @SuppressWarnings("serial")
    public class AtomikosJtaPlatform extends AbstractJtaPlatform {
    
        private static TransactionManager transactionManager;
        private static UserTransaction userTransaction;
    
        public static void factoryInit(TransactionManager transactionManager, UserTransaction userTransaction) {
            AtomikosJtaPlatform.transactionManager = transactionManager;
            AtomikosJtaPlatform.userTransaction = userTransaction;
        }
    
        @Override
        protected TransactionManager locateTransactionManager() {
            return transactionManager;
        }
    
        @Override
        protected UserTransaction locateUserTransaction() {
            return userTransaction;
        }
    
    }
    

    Then in Spring configuration:

        
            
            
        
    
        
            
        
    
         
        
            
            
            
                
                    
                    
                
            
        
    
    
            
                
                    com.mycompnay.a.b.AtomikosJtaPlatform
     ...
    

提交回复
热议问题