使用JTA TransactionManager使用Spring3 + Hibernate4“找不到当前线程的会话”

时间:2012-10-25 23:25:18

标签: sql-server-2008 spring-mvc jboss7.x spring-3 hibernate-4.x

我正在尝试从

迁移

spring 1.2.9 with hibernate 3.2.0.ga

spring 3.1.1.RELEASE with hibernate 4.0.1.Final

大多数 bean布线都是使用xml 而不是注释完成的。在部署之前,我已对dao,service和controller对象进行了必要的更改以及单元测试,以确保现有spring配置的兼容性。

当我部署到JBoss AS 7.1.1.Final时,我得到"找不到当前线程的会话"。我一直在寻找通过这个论坛和spring和hibernate文档的答案,我仍然无法找到适用于我的问题的解决方案。

这是hibernate异常日志

> 06:18:25,703 ERROR [com.test.customer.dao.InvoicingInfoDAOImpl] (http--12
7.0.0.1-8080-1) selectBillingInfoIdById:Runtime error.: org.hibernate.HibernateE
xception: **No Session found for current thread**
        at org.springframework.orm.hibernate4.SpringSessionContext.currentSessio
n(SpringSessionContext.java:97) [spring-orm-3.1.1.RELEASE.jar:3.1.1.RELEASE]
        at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFa
ctoryImpl.java:883) [hibernate-core-4.0.1.Final.jar:4.0.1.Final]
        at com.test.customer.dao.InvoicingInfoDAOImpl.selectBillingInfoBy
Id(InvoicingInfoDAOImpl.java:58) [classes:]
        at com.test.customer.services.CustomerServiceImpl.getBillingInfo(
CustomerServiceImpl.java:535) [classes:]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1
.6.0_25]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39) [rt.jar:1.6.0_25]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25) [rt.jar:1.6.0_25]
        at java.lang.reflect.Method.invoke(Method.java:597) [rt.jar:1.6.0_25]
        at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflecti
on(AopUtils.java:318) [spring-aop-3.1.1.RELEASE.jar:3.1.1.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJo
inpoint(ReflectiveMethodInvocation.java:183) [spring-aop-3.1.1.RELEASE.jar:3.1.1
.RELEASE]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(
ReflectiveMethodInvocation.java:150) [spring-aop-3.1.1.RELEASE.jar:3.1.1.RELEASE
]
        at org.springframework.transaction.interceptor.TransactionInterceptor.in
voke(TransactionInterceptor.java:110) [spring-tx-3.1.1.RELEASE.jar:3.1.1.RELEASE
]
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(
ReflectiveMethodInvocation.java:172) [spring-aop-3.1.1.RELEASE.jar:3.1.1.RELEASE
]
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynami
cAopProxy.java:202) [spring-aop-3.1.1.RELEASE.jar:3.1.1.RELEASE]
        at $Proxy22.getBillingInfo(Unknown Source)      at com.test.custo
mer.CustomerController.getBillingInfo(CustomerController.java:205) [classes:]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1
.6.0_25]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39) [rt.jar:1.6.0_25]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25) [rt.jar:1.6.0_25]
        at java.lang.reflect.Method.invoke(Method.java:597) [rt.jar:1.6.0_25]
        at org.springframework.web.method.support.InvocableHandlerMethod.invoke(
InvocableHandlerMethod.java:213) [spring-web-3.1.1.RELEASE.jar:3.1.1.RELEASE]
        at org.springframework.web.method.support.InvocableHandlerMethod.invokeF
orRequest(InvocableHandlerMethod.java:126) [spring-web-3.1.1.RELEASE.jar:3.1.1.R
ELEASE]
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocabl
eHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96) [spring-we
bmvc-3.1.1.RELEASE.jar:3.1.1.RELEASE]
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingH
andlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617) [spring
-webmvc-3.1.1.RELEASE.jar:3.1.1.RELEASE]
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingH
andlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578) [spring-webm
vc-3.1.1.RELEASE.jar:3.1.1.RELEASE]
        at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapt
er.handle(AbstractHandlerMethodAdapter.java:80) [spring-webmvc-3.1.1.RELEASE.jar
:3.1.1.RELEASE]
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(Dispatch
erServlet.java:923) [spring-webmvc-3.1.1.RELEASE.jar:3.1.1.RELEASE]
        at org.springframework.web.servlet.DispatcherServlet.doService(Dispatche
rServlet.java:852) [spring-webmvc-3.1.1.RELEASE.jar:3.1.1.RELEASE]
        at org.springframework.web.servlet.FrameworkServlet.processRequest(Frame
workServlet.java:882) [spring-webmvc-3.1.1.RELEASE.jar:3.1.1.RELEASE]
        at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServ
let.java:789) [spring-webmvc-3.1.1.RELEASE.jar:3.1.1.RELEASE]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:754) [jboss-s
ervlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-s
ervlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:275) [jbossweb-7.0.13.Final.jar:]
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:161) [jbossweb-7.0.13.Final.jar:]
        at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(Secu
rityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]

这是会话工厂和事务管理器的弹簧配置(这里使用jta)

> <!-- Configuration of Hibernate SessionFactory -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="csvliteDataSource" />
        </property>
        <property name="hibernateProperties">
            <ref bean="csvliteHibernateProperties" />
        </property>
        <property name="mappingResources">
            <list>
                <!-- Customer Domain Mapping -->
                <value>com/test/customer/domain/InvoicingInfo.hbm.xml</value>
                <value>com/test/customer/domain/PickupAddress.hbm.xml</value>
            </list>
        </property>
    </bean>

    <!-- Configurationof Hibernate Configuration Properties -->
    <bean id="csvliteHibernateProperties" class="java.util.Properties">
        <constructor-arg>
            <props>
                <!-- Hibernate Configuration Properties -->
                <prop key="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect</prop>             
                <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
                <!-- Hibernate Configuration Properties - enabled only if debugging -->
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.generate_statistics">true</prop>
                <prop key="hibernate.use_sql_comments">true</prop>
                <!-- Hibernate Cache Properties -->
                <prop key="hibernate.cache.provider_class">org.hibernate.cache.OSCacheProvider</prop>
                <prop key="hibernate.cache.use_query_cache">true</prop>         
            </props>
        </constructor-arg>
    </bean>

    <!-- Configuration of Data Source -->
    <bean id="csvliteDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName">
            <value>java:jboss/datasources/CSLiteDS</value>
        </property>
    </bean>

    <!-- Configuration of Transaction Manager -->
    <bean id="csvliteTxManager"
        class="org.springframework.transaction.jta.JtaTransactionManager">
        <property name="transactionManagerName">
            <value>java:jboss/TransactionManager</value>
        </property>
    </bean>

以下是相关dao和服务对象的spring配置(它为每个服务对象使用TransactionProxyFactoryBean)

<bean id="invoicingInfoDAO"
    class="com.test.customer.dao.InvoicingInfoDAOImpl">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

<bean id="pickupAddressDAO"
    class="com.test.customer.dao.PickupAddressDAOImpl">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

<bean id="customerService"
    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager" ref="csvliteTxManager"/>
    <property name="target" ref="customerServiceImpl"/>
    <property name="transactionAttributes"> 
        <props>     
            <prop key="getBillingInfo">PROPAGATION_REQUIRES_NEW, -Throwable</prop>                                              
            <prop key="addBillingInfo">PROPAGATION_REQUIRED, -Throwable</prop>
            <prop key="updateBillingInfo">PROPAGATION_REQUIRED, -Throwable</prop>
            <prop key="addPickupAddress">PROPAGATION_REQUIRED, -Throwable</prop>
            <prop key="updatePickupAddress">PROPAGATION_REQUIRED, -Throwable</prop>
            <prop key="getPickupAddress">PROPAGATION_REQUIRES_NEW, -Throwable</prop>
            <prop key="unlockPickupAddress">PROPAGATION_REQUIRED, -Throwable</prop>
            <prop key="lockPickupAddress">PROPAGATION_REQUIRED, -Throwable</prop>
            <prop key="lockBillingInfo">PROPAGATION_REQUIRED, -Throwable</prop>
            <prop key="unlockBillingInfo">PROPAGATION_REQUIRED, -Throwable</prop>
            <prop key="closePickupAddress">PROPAGATION_REQUIRED, -Throwable</prop>
            <prop key="processPurgePickupAddress">PROPAGATION_REQUIRED, -Throwable</prop>
            <prop key="getMultiplePickupAddress">PROPAGATION_REQUIRES_NEW, -Throwable</prop>           
        </props>
    </property>     
</bean> 
<bean id="customerServiceImpl" class="com.test.customer.services.CustomerServiceImpl">
    <property name="invoicingInfoDAO" ref="invoicingInfoDAO"/>  
    <property name="pickupAddressDAO" ref="pickupAddressDAO"/>
</bean>

我在配置中错过了什么?

=========================

[更新 - 20121101]

我接受了@ adrian-shum的建议,并通过网络上的一些信息交叉引用。

我做了以下测试,但除非我切换到HibernateTransaction而不是JTA事务,否则所有测试都无法正常工作。

测试1 - 成功

(使用HibernateTransactionManager)

<!-- Configurationof Hibernate Configuration Properties -->
    <bean id="csvliteHibernateProperties" class="java.util.Properties">
        <constructor-arg>
            <props>
                <!-- Hibernate Configuration Properties -->
                <prop key="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect</prop>             
                <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>

        <!-- Hibernate Configuration Properties - enabled only if debugging -->
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.generate_statistics">true</prop>
                <prop key="hibernate.use_sql_comments">true</prop>
                <!-- Hibernate Cache Properties -->
                <prop key="hibernate.cache.provider_class">org.hibernate.cache.OSCacheProvider</prop>
                <prop key="hibernate.cache.use_query_cache">true</prop>         
            </props>
        </constructor-arg>
    </bean>

    <!-- Configuration of Data Source -->
    <bean id="csvliteDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName">
            <value>java:jboss/datasources/CSLiteDS</value>
        </property>
    </bean>

    <!-- Configuration of Transaction Manager -->
    <bean id="csvliteTxManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

TEST 2 - FAILED

(使用JtaTransactionManager - hibernate.transaction.manager_lookup_class + hibernate.transaction.factory_class)

<!-- Configurationof Hibernate Configuration Properties -->
    <bean id="csvliteHibernateProperties" class="java.util.Properties">
        <constructor-arg>
            <props>
                <!-- Hibernate Configuration Properties -->
                <prop key="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect</prop>             
                <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
                <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JtaTransactionFactory</prop>
                <prop key="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</prop>

                <!-- Hibernate Configuration Properties - enabled only if debugging -->
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.generate_statistics">true</prop>
                <prop key="hibernate.use_sql_comments">true</prop>
                <!-- Hibernate Cache Properties -->
                <prop key="hibernate.cache.provider_class">org.hibernate.cache.OSCacheProvider</prop>
                <prop key="hibernate.cache.use_query_cache">true</prop>         
            </props>
        </constructor-arg>
    </bean>

    <!-- Configuration of Data Source -->
    <bean id="csvliteDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName">
            <value>java:jboss/datasources/CSLiteDS</value>
        </property>
    </bean>

    <!-- Configuration of Transaction Manager -->   
    <bean id="csvliteTxManager"
        class="org.springframework.transaction.jta.JtaTransactionManager">
        <property name="transactionManagerName">
            <value>java:jboss/TransactionManager</value>
        </property>
    </bean>

TEST 3 - FAILED

(使用JtaTransactionManager - hibernate.transaction.jta.platform属性)

<!-- Configurationof Hibernate Configuration Properties -->
    <bean id="csvliteHibernateProperties" class="java.util.Properties">
        <constructor-arg>
            <props>
                <!-- Hibernate Configuration Properties -->
                <prop key="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect</prop>             
                <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
                <!-- prop key="hibernate.transaction.jta.platform">org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform</prop-->
                <prop key="hibernate.transaction.jta.platform">org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform</prop>

                <!-- Hibernate Configuration Properties - enabled only if debugging -->
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.generate_statistics">true</prop>
                <prop key="hibernate.use_sql_comments">true</prop>
                <!-- Hibernate Cache Properties -->
                <prop key="hibernate.cache.provider_class">org.hibernate.cache.OSCacheProvider</prop>
                <prop key="hibernate.cache.use_query_cache">true</prop>         
            </props>
        </constructor-arg>
    </bean>

    <!-- Configuration of Data Source -->
    <bean id="csvliteDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName">
            <value>java:jboss/datasources/CSLiteDS</value>
        </property>
    </bean>

    <!-- Configuration of Transaction Manager -->
    <bean id="csvliteTxManager"
        class="org.springframework.transaction.jta.JtaTransactionManager">
        <property name="transactionManagerName">
            <value>java:jboss/TransactionManager</value>
        </property>
    </bean>

使用Hibernate Transaction不是我的选择。为了使JTA事务有效,我还需要在JBoss AS 7端做些什么吗?

=========================

[更新 - 20121101]

基本上,我没有在日志中找到任何其他异常,除了我之前发布的异常。

然而,有趣的是我没有看到任何显示事务管理器与我指定的JNDI的绑定的日志条目,它总是说它将使用&#34; HHH000399:使用默认事务策略(直接JDBC事务)&#34;

15:46:00,548 INFO  [org.springframework.transaction.jta.JtaTransactionManager] (
MSC service thread 1-5) Using JTA UserTransaction: org.jboss.tm.usertx.client.Se
rverVMClientUserTransaction@307c44
15:46:00,548 INFO  [org.springframework.transaction.jta.JtaTransactionManager] (
MSC service thread 1-5) Using JTA TransactionManager: com.arjuna.ats.jbossatx.jt
a.TransactionManagerDelegate@a2f435
15:46:00,548 INFO  [org.springframework.transaction.jta.JtaTransactionManager] (
MSC service thread 1-5) Using JTA TransactionSynchronizationRegistry: com.arjuna
.ats.internal.jta.transaction.arjunacore.TransactionSynchronizationRegistryImple
@4ef523
15:46:01,736 INFO  [org.hibernate.annotations.common.Version] (MSC service threa
d 1-5) HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
15:46:01,767 INFO  [org.hibernate.Version] (MSC service thread 1-5) HHH000412: H
ibernate Core {4.0.1.Final}
15:46:01,783 INFO  [org.hibernate.cfg.Environment] (MSC service thread 1-5) HHH0
00206: hibernate.properties not found
15:46:01,783 INFO  [org.hibernate.cfg.Environment] (MSC service thread 1-5) HHH0
00021: Bytecode provider name : javassist
15:46:06,142 INFO  [org.hibernate.dialect.Dialect] (MSC service thread 1-5) HHH0
00400: Using dialect: org.hibernate.dialect.SQLServer2008Dialect
15:46:06,189 INFO  [org.hibernate.engine.transaction.internal.TransactionFactory
Initiator] (MSC service thread 1-5) HHH000399: Using default transaction strateg
y (direct JDBC transactions)
15:46:06,205 INFO  [org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory] (M
SC service thread 1-5) HHH000397: Using ASTQueryTranslatorFactory
15:46:06,361 INFO  [org.hibernate.validator.util.Version] (MSC service thread 1-
5) Hibernate Validator 4.2.0.Final

不确定这是否是原因......如果是,我还需要做什么?

=========================

[更新 - 20130425]

我怀疑这可能与未配置的XA数据源有关。我将测试并确认。

如果有人能告诉我可能的原因,我仍然感激不尽。

[更新 - 20130426]

我已按如下方式配置XA数据源,但我仍然遇到相同的错误。我尝试过的另一件事是升级到使用hibernate 4.2,我仍然会遇到同样的错误。

是否有人尝试使用JBoss7.1.1.final + Spring3.1.1.RELEASE + Hibernate4.0.1.Final + MS SQL server 2008配置全局JTA事务支持?

<subsystem xmlns="urn:jboss:domain:datasources:1.0">
  <datasources>
    <xa-datasource jta="true" jndi-name="java:jboss/datasources/CSLiteDS" pool-name="CSLiteDS" enabled="true" use-java-context="true">
      <xa-datasource-property name="ServerName">{someServerName}</xa-datasource-property>
      <xa-datasource-property name="PortNumber">{somePortNo}</xa-datasource-property>
      <xa-datasource-property name="DatabaseName">{someDatabaseName}</xa-datasource-property>
      <xa-datasource-property name="SelectMethod">cursor</xa-datasource-property>
      <driver>sqljdbc</driver>
      <security>
        <user-name>{someUserName}</user-name>
        <password>{somePassword}</password>
      </security>
    </xa-datasource>
    <xa-datasource jta="true" jndi-name="java:jboss/datasources/CRDDataSource" pool-name="CRDDataSource" enabled="true" use-java-context="true">
      <xa-datasource-property name="ServerName">{someServerName}</xa-datasource-property>
      <xa-datasource-property name="PortNumber">{somePortNo}</xa-datasource-property>
      <xa-datasource-property name="DatabaseName">{someDatabaseName}</xa-datasource-property>
      <xa-datasource-property name="SelectMethod">cursor</xa-datasource-property>
      <driver>sqljdbc</driver>
      <security>
        <user-name>{someUserName}</user-name>
        <password>{somePassword}</password>
      </security>
    </xa-datasource>
    <drivers>
      <driver name="sqljdbc" module="com.microsoft.sqlserver.jdbc">
        <driver-class>com.microsoft.sqlserver.jdbc.SQLServerDriver</driver-class>
        <xa-datasource-class>com.microsoft.sqlserver.jdbc.SQLServerXADataSource</xa-datasource-class>
      </driver>
    </drivers>
  </datasources>
</subsystem>

2 个答案:

答案 0 :(得分:1)

我通过在hibernate sessionfactory中设置属性jtaTransactionManager

解决了这个问题
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="packagesToScan" value="com.foo" />
        <property name="dataSource" ref="myDataSource" />
        <property name="jtaTransactionManager" ref="transactionManager" />
...
</bean>

答案 1 :(得分:0)

只是一个简短的扫描,我认为你必须在Hibernate的属性中定义hibernate.transaction.manager_lookup_class + hibernate.transaction.factory_class或hibernate.transaction.jta.platform属性。