首先,我不能使用Spring的@Transactional注释。我必须确切地使用JTA,它在EJB容器中。目前,我正在使用JBoss AS 7.0 Web-Profile。因此,我需要做的是配置Hibernate的会话工厂,使其在JTA的环境中正确地与Spring事务一起使用。我当前的配置:

context.xml配置的一部分:

<tx:annotation-driven/>
<tx:jta-transaction-manager  />

<!-- Some other beans -->

<bean id="userTransaction" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/UserTransaction"></property>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
        <list>
            <value>com.badmitrii.db.entity.Employee</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
            <prop key="hibernate.show_sql">false</prop>
        </props>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
    <property name="userTransaction" ref="userTransaction"></property>
</bean>


DAO方法:

public Player getPlayerById(Integer id){
    try {
        userTransaction.begin();
    } catch (Exception e) { }

    //Here is obtaining a Criteria object and setting Restrictions

    try {
        userTransaction.commit();
    } catch (Exception e) { }
    return (Player) criteria.uniqueResult();
}


但是,当我尝试使用DAO方法获取Session时,得到了以下例外:

org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
    org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:134)
    org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1024)
    com.badmitrii.db.dao.EmployeeDAOImpl.getEmployeeById(EmployeeDAOImpl.java:34)
    com.badmitrii.EmployeeListController.getEmployeeById(EmployeeListController.java:42)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:483)
    org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:847)


如何正确配置?

最佳答案

首先,删除JtaTransactionManager的声明,因为<tx:jta-transaction-manager />已经提供了该声明。

下一步,没有理由不能在声明性TX管理的全部要点的JTA环境中使用@Transactional

您应该将已配置的jta transactionmanager连接到LocalSessionFactoryBean以切换使用的CurrentSessionContext

<tx:annotation-driven/>
<tx:jta-transaction-manager  />

<!-- Some other beans -->

<bean id="userTransaction" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/UserTransaction"></property>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="jtaTransactionManager" ref="transactionManager" />
    <property name="annotatedClasses">
        <list>
            <value>com.badmitrii.db.entity.Employee</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
            <prop key="hibernate.show_sql">false</prop>
        </props>
    </property>
</bean>


然后,在您的鳕鱼中,您只需执行以下操作

@Transactional
public Player getPlayerById(Integer id){
    //Here is obtaining a Criteria object and setting Restrictions
    return (Player) criteria.uniqueResult();
}


更新:

对于JBoss,由于JTA的<tx:jta-transaction-manager />在JNDI中以名称TransactionManager而不是well-known names之一注册的事实,因此java:jboss/TransactionManager不起作用。您将需要自己声明JtaTransactionManager bean并删除<tx:jta-transaction-manager />元素。对于查找,您需要指定transactionManagerName或自己进行JNDI查找。

<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
    <property name="transactionManagerName" value="java:jboss/TransactionManager" />
</bean>


UserTransaction以默认名称注册,因此您可以在JtaTransactionManager中省略对它的注入,因为它会自己进行查找。

关于java - 我的配置spring sessionFactory配置有什么问题?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27835633/

10-10 13:31