1

我正在尝试从

春季 1.2.9 与休眠 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 时,我得到“No Session found for current thread”。我一直在通过这个论坛和 spring 和 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]

这是会话工厂和事务管理器的spring配置(这里使用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>

测试 2 - 失败

(使用 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>

测试 3 - 失败

(使用 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 的绑定,并且它总是说它将使用“HHH000399:使用默认事务策略(直接 JDBC 事务)”

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 数据源,但我仍然遇到同样的错误。我尝试过的另一件事是升级以使用休眠 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>
4

2 回答 2

1

我通过在休眠会话工厂中设置属性 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>
于 2012-11-06T16:18:28.953 回答
0

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

于 2012-10-26T04:11:38.617 回答