编程语言
首页 > 编程语言> > java-为什么我的(spring)HibernateTransactionManager无法在检票口工作?

java-为什么我的(spring)HibernateTransactionManager无法在检票口工作?

作者:互联网

我试图将其简化为我认为相关的内容,我希望它足够且不至于使人感到压倒.请帮忙!

我正在将一个小型wicket databinder休眠Web应用程序转换为使用wicket spring休眠.我有一个DAO服务类,由Spring注入了一个休眠的SessionFactory.我可以使用会话工厂执行只读操作(默认情况下,自动提交处于启用状态).我想做的是使用HibernateTransactionManager和@Transactional批注进行事务性操作.

我定义了一个DAO服务实现,该实现在标记为@Transactional的方法中使用注入的SessionFactory:

public class DAO implements IDAO {
 @SpringBean
 private SessionFactory sessionFactory;

 public DAO() {
  super();
 }

 @Transactional
 public Object execute(SessionUnit sessionUnit) {
  Session sess = sessionFactory.getCurrentSession();
  Object result;
  result = sessionUnit.run(sess);
  sess.flush();
  return result;
 }

 public void setSessionFactory(SessionFactory sessionFactory) {
  this.sessionFactory = sessionFactory;
 }

 @Transactional
 public boolean isObjectPersistent(Object object) {
  return sessionFactory.getCurrentSession().contains(object);
 }
}

当我尝试调用isObjectPersistent()时,由于没有人调用session.beginTransaction(),我得到了一个休眠异常:

Caused by: org.hibernate.HibernateException: contains is not valid without active transaction
 at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:338)
 at $Proxy38.contains(Unknown Source)
 at com.gorkwobbler.shadowrun.karma.db.hibernate.DAO.isObjectPersistent(DAO.java:35)
(reflection stuff omitted...)
 at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
 at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
 at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
(reflection stuff omitted...)
 at org.apache.wicket.proxy.LazyInitProxyFactory$JdkHandler.invoke(LazyInitProxyFactory.java:416)
 at org.apache.wicket.proxy.$Proxy36.isObjectPersistent(Unknown Source)

我还从完整的堆栈跟踪中注意到,OpenSessionInViewFilter正在被调用,我不确定这是否相关.让我知道您是否需要其余的堆栈跟踪.

如果我创建一个自定义的WebRequestCycle子类来开始事务,那么我可以克服这个问题.在我看来,这似乎破坏了@Transactional的目的,而我的实现也被证明是有问题的.

这是我的applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Reference: http://wicketinaction.com/2009/06/wicketspringhibernate-configuration/ -->
<beans default-autowire="autodetect"
    xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <!-- bean definitions -->
    <bean id="wicketApplication" class="com.gorkwobbler.shadowrun.karma.view.wicket.core.WicketApplication" />

    <bean id="placeholderConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreUnresolvablePlaceholders" value="false" />
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        <property name="ignoreResourceNotFound" value="false" />
        <property name="locations">
            <list>
                <value>classpath*:/application.properties</value>
            </list>
        </property>
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName">
            <value>${jdbc.driver}</value>
        </property>
        <property name="url">
            <value>${jdbc.url}</value>
        </property>
        <property name="username">
            <value>${jdbc.username}</value>
        </property>
        <property name="password">
            <value>${jdbc.password}</value>
        </property>
    </bean>

    <tx:annotation-driven transaction-manager="txManager" />

    <!-- setup transaction manager  -->
    <bean id="txManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>

    <!-- hibernate session factory -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="configLocation">
            <value>classpath:/hibernate.cfg.xml</value>
        </property>
        <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties">
            <props>
            </props>
        </property>
        <property name="packagesToScan">
            <list>
                <value>com.gorkwobbler.shadowrun.karma.domain</value>
                <value>com.gorkwobbler.shadowrun.karma.domain.*</value>
            </list>
        </property>
    </bean>

    <bean id="dao"
        class="com.gorkwobbler.shadowrun.karma.db.hibernate.DAO">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>

    <!-- Don't know what this is for, but it was in the sample config I started from --> 
    <!-- <context:component-scan base-package="com.gorkwobbler.shadowrun.karma" />  -->
</beans>

我怎样才能让我的DAO开始事务,在该方法结束时提交或在发生错误时回滚?我想使用尽可能最小/标准的配置;如果可以的话,我更喜欢注释而不是XML.

编辑:

我修改了上面的applicationContext,以删除仍然无法运行的AOP配置内容.

使用调试器,我确定存储在TransactionInterceptor的会话持有者映射中的SessionImpl与当我调用sessionFactory.getCurrentSession()时在DAO方法中检索的SessionImpl不是同一会话.谁能解释为什么?我究竟做错了什么?魔术不起作用. =(

编辑

启动过程中,我还在控制台中注意到以下消息:

WARN  - stractEhcacheRegionFactory - No TransactionManagerLookup found in Hibernate config, XA Caches will be participating in the two-phase commit!

解决方法:

事实证明,问题实际上出在我发布的配置信息中.抱歉!

我上面的配置链接到一个外部化的hibernate.cfg.xml,它声明了以下属性:

    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>

我必须已经从某个示例休眠配置文件中的某处复制了此文件.此属性导致我的SessionFactory忽略spring提供的会话上下文,并在其位置使用线程本地上下文.删除此属性可解决问题(如果未指定,则默认情况下,Hibernate使用JTA上下文).

标签:hibernate,transactions,wicket,spring,java
来源: https://codeday.me/bug/20191023/1915876.html