2014-01-07 56 views
2

您好我有一個使用spring和hibernate的Java web應用程序。bitronix-無法找到目前的JTA交易

我有一個模型類叫做角色。對於所有的模型類有共同GenericDao.java

public interface GenericDao<T>{ 
    public void insert(T transientInstance);  
    public T findById(Class<?> clazz, Serializable id); 
} 

GenericHibernateDao.java

@Repository 
public class GenericHibernateDao<T extends Serializable> 
implements GenericDao<T>{ 

    @Resource 
    protected SessionFactory sessionFactory; 

    @Override 
    public void insert(T transientInstance) { 
     sessionFactory.getCurrentSession().persist(transientInstance); 
    } 
     @SuppressWarnings("unchecked") 
    @Override 
    public T findById(Class<?> clazz, Serializable id) { 
     return (T) sessionFactory.getCurrentSession().get(clazz, id); 
     } 
} 

RoleService.java

public interface RoleService { 
    public void insert(Role role); 
    public Role findById(Integer id); 
} 

,並擁有實現RoleServiceImpl。 java

@Service 
public class RoleServiceImpl implements RoleService { 

    @Autowired 
    private GenericDao<Role> roleDao; 

    @Override 
    public void insert(Role role) { 
     roleDao.insert(role); 
    } 

    @Override 
    public Role findById(Integer id) { 
     return roleDao.findById(Role.class, id); 
    } 
} 

我有一個控制器調用該findbyid methid和傳遞價值 - RegisterController.java

@Controller 
public class UserRegisterController { 

    @Autowired 
    public RoleService roleService; 

    @RequestMapping(value = "/register", method = RequestMethod.POST, consumes= MediaType.APPLICATION_JSON_VALUE) 
    @ResponseBody 
    public void registerUser(@RequestBody User user) 
    { 
     Role role=new Role(); 
     role=roleService.findById(1); 
     System.out.println("Saved"); 
    } 
} 

最後我春天的applicationContext.xml

<bean id="dataSource" class="bitronix.tm.resource.jdbc.PoolingDataSource" 
     init-method="init" destroy-method="close"> 
     <property name="className" 
      value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" /> 
     <property name="uniqueName" value="ocr/DocumentDatabase" /> 
     <property name="minPoolSize" value="1" /> 
     <property name="maxPoolSize" value="5" /> 
     <property name="automaticEnlistingEnabled" value="true" /> 
     <property name="allowLocalTransactions" value="true" /> 
     <property name="useTmJoin" value="true" /> 
     <property name="shareTransactionConnections" value="true" /> 
     <property name="driverProperties"> 
      <props> 
       <prop key="url">${jdbc.url}</prop> 
       <prop key="user">${jdbc.username}</prop> 
       <prop key="password">${jdbc.password}</prop> 
      </props> 
     </property> 
    </bean> 
    <bean id="bitronixTransactionManager" factory-method="getTransactionManager" 
     class="bitronix.tm.TransactionManagerServices" depends-on="btmConfig, dataSource" 
     destroy-method="shutdown" /> 

    <bean id="transactionManager" 
     class="org.springframework.transaction.jta.JtaTransactionManager"> 
     <property name="transactionManager" ref="bitronixTransactionManager" /> 
     <property name="userTransaction" ref="bitronixTransactionManager" /> 
    </bean> 
    <tx:annotation-driven transaction-manager="transactionManager" /> 

    <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" 
     destroy-method="destroy"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="jtaTransactionManager" ref="transactionManager"/> 
     <property name="configLocation" value="classpath:hibernate.cfg.xml" /> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
       <prop key="hibernate.query.substitutions">true 'Y', false 'N'</prop> 
      </props> 
     </property> 
    </bean> 

I在tomcat運行此應用程序。它給出了以下錯誤。

SEVERE: Servlet.service() for servlet [myappdemo] in context with path [/myappdemo] threw exception [Request processing failed; nested exception is org.hibernate.HibernateException: Unable to locate current JTA transaction] with root cause 
org.hibernate.HibernateException: Unable to locate current JTA transaction 
    at org.hibernate.context.internal.JTASessionContext.currentSession(JTASessionContext.java:88) 
    at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:90) 
    at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:980) 
    at com.myapp.myappdemo.dao.hibernate.GenericHibernateDao.findById(GenericHibernateDao.java:46) 
    at com.myapp.myappdemo.dao.hibernate.GenericHibernateDao.findById(GenericHibernateDao.java:15) 
    at com.myapp.myappdemo.service.impl.RoleServiceImpl.findById(RoleServiceImpl.java:38) 
    at com.myapp.myappdemo.web.UserRegisterController.registerUser(UserRegisterController.java:36) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219) 
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) 
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) 
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745) 
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686) 
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) 
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925) 
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856) 
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:920) 
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:827) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647) 
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:801) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
    at java.lang.Thread.run(Thread.java:744) 

當我打電話了roleService.findById(1);

它爲什麼發生這種事?我該如何解決?

我使用彈簧3.2.1.RELEASE,休眠4.2.0.Final,BTM 2.1.3

回答

5

SessionFactory則是配置爲使用JtaTransactionManager中。當你這樣做時,hibernate足夠聰明,可以查看當前事務資源來查找會話。

您的彈簧配置允許您使用註釋來執行事務劃分。

您的事務管理器似乎配置正確。

所以,一切都看起來不錯。唯一缺少的部分是交易註釋!

只是在你的服務層添加@Transactional

@Override 
@Transactional 
public Role findById(Integer id) { 
    return roleDao.findById(Role.class, id); 
} 

(你也可以把服務類此批註)

+0

在服務類我把。那次我得到'transaction timedout exception' –

+1

所以看起來'無法找到當前的JTA事務'是固定的。請接受答案並詢問有關交易超時的另一個問題(如果您尚未解決問題)。不要忘了發佈你的spring配置(比如在這篇文章+ btmConfig bean)和stacktrace。 – ben75