java – org.hibernate.HibernateException:在hibernate升级时没有指定TransactionManagerLookup

前端之家收集整理的这篇文章主要介绍了java – org.hibernate.HibernateException:在hibernate升级时没有指定TransactionManagerLookup前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在运行spring 4.1.4,hibernate 4.3.8,atomikos 3.9.3,java 8,tomcat 8.

当我启动服务器时,我在localhost.log中看到了上述异常,但我不知道在哪里配置TransactionManagerLookup,而不是我已经在配置它的地方.在升级hibernate之前没有发生这种情况,因此很可能是版本问题.有人可以帮忙吗?

仅供参考:catalina.out显示没什么用处.只是:

SEVERE [localhost-startStop-1] org.apache.catalina.core.StandardContext.startInternal Error listenerStart
SEVERE [localhost-startStop-1] org.apache.catalina.core.StandardContext.startInternal Context [] startup Failed due to prevIoUs errors

我的appContext定义:

并且完整的堆栈跟踪是:

22-Jan-2015 10:07:25.734 SEVERE [localhost-startStop-1] org.apache.catalina.core.StandardContext.listenerStart Exception sending context initialized event to listener instance of class com.my.app.web.InitializerListener
 org.hibernate.HibernateException: No TransactionManagerLookup specified
    at org.hibernate.context.internal.JTASessionContext.currentSession(JTASessionContext.java:85)
    at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1014)
    at com.my.app.dao.AbstractMyDAO.currentSession(AbstractMyDAO.java:116)
    at com.my.app.dao.AbstractMyDAO.criteria(AbstractMyDAO.java:86)
    at com.my.app.dao.AbstractMyDAO.count(AbstractMyDAO.java:79)
    at com.my.app.initialize.MyInitializerImpl.initializeApplicaiton(MyInitializerImpl.java:69)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
    at com.sun.proxy.$Proxy220.initializeApplicaiton(Unknown Source)
    at com.my.app.web.MyInitializerListener.contextInitialized(MyInitializerListener.java:42)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4772)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5196)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:714)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:917)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1701)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

编辑:Atomikos事务管理器配置如此

编辑2:

我想我需要在这里澄清一些事情.
我有一个名为AbstractMyDAO的DAO对象(从堆栈中可以看到).在这个对象中定义了一个会话工厂

@Autowired
private SessionFactory mySessionFactory;

调用currentSession时,抛出上述错误,因为hibernate无法找到与此会话工厂关联的事务管理器查找.

public Session currentSession() {
    return mySessionFactory.getCurrentSession();
}

此会话工厂在我的应用程序上下文文件中定义如下:

factorybean">

    sql">${my.hibernate.showsql}

正如您所看到的,属性hibernate.transaction.manager_lookup_class正在定义,但在创建bean时无法找到,我不明白为什么.配置是否已更改?

编辑3:
当我调试时:
    final JtaPlatform jtaPlatform = factory().getServiceRegistry().getService(JtaPlatform.class);

它返回NoJTAPlatform.我想这是我的问题.

编辑4:
似乎没有任何东西可以实现适合tomcat的JtaPlatform或者我是精神上的吗?

最佳答案
确保您也配置了Atomikos事务管理器,因此TransactionManagerLookup可以相应地找到UserTransaction:

然后,您也应该使用Spring EntityManager工厂:

factorybean"
    p:dataSource-ref="dataSource"
    p:persistenceXmlLocation="**/persistence.xml"
    p:persistenceUnitName="persistenceUnit" 
    depends-on="dataSource,transactionManager">
    

那么DAO应该注入EntityManager:

@PersistenceContext(unitName = "persistenceUnit")
private EntityManager entityManager;

而不是直接调用currentSession(AbstractMyDAO):

at com.my.app.dao.AbstractMyDAO.currentSession(AbstractMyDAO.java:116)

猜你在找的Spring相关文章