Hibernate:无法访问 TransactionManager 或 UserTransaction 以进行物理事务委托
Posted
技术标签:
【中文标题】Hibernate:无法访问 TransactionManager 或 UserTransaction 以进行物理事务委托【英文标题】:Hibernate: Unable to access TransactionManager or UserTransaction to make physical transaction delegate 【发布时间】:2015-12-06 23:34:10 【问题描述】:我使用 Servlet、PostgreSQL 和 Hibernate 与 EntityManager 构建 Web 应用程序,一切正常,直到我将项目移至基于 Maven 的项目。现在,每次尝试从 EntityManagerFactory 创建 EntityManager 时,我都会遇到这个奇怪的异常:
org.hibernate.resource.transaction.backend.jta.internal.JtaPlatformInaccessibleException: Unable to access TransactionManager or UserTransaction to make physical transaction delegate
at org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl.makePhysicalTransactionDelegate(JtaTransactionCoordinatorImpl.java:229)
at org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl.getTransactionDriverControl(JtaTransactionCoordinatorImpl.java:203)
at org.hibernate.engine.transaction.internal.TransactionImpl.<init>(TransactionImpl.java:36)
at org.hibernate.internal.AbstractSessionImpl.getTransaction(AbstractSessionImpl.java:309)
at org.hibernate.internal.SessionImpl.<init>(SessionImpl.java:277)
at org.hibernate.internal.SessionFactoryImpl$SessionBuilderImpl.openSession(SessionFactoryImpl.java:1322)
at org.hibernate.jpa.internal.EntityManagerImpl.internalGetSession(EntityManagerImpl.java:133)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.postInit(AbstractEntityManagerImpl.java:174)
at org.hibernate.jpa.internal.EntityManagerImpl.<init>(EntityManagerImpl.java:83)
at org.hibernate.jpa.internal.EntityManagerFactoryImpl.internalCreateEntityManager(EntityManagerFactoryImpl.java:311)
at org.hibernate.jpa.internal.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:278)
at cl.bicubic.App.getEntityManager(App.java:100)
at cl.bicubic.dao.DAO.<init>(DAO.java:24)
at cl.bicubic.dao.UserAtomicDAO.<init>(UserAtomicDAO.java:15)
at cl.bicubic.Application.init(Application.java:70)
at javax.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1241)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1154)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:827)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:526)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:655)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
这里是Hibernate在持久化单元中的配置:
<properties>
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost/mydb?charSet=UTF-8"/>
<property name="hibernate.connection.user" value="dbuser"/>
<property name="hibernate.connection.password" value="dbpass"/>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
<property name="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL9Dialect"/>
<property name="hibernate.c3p0.min_size" value="5"/>
<property name="hibernate.c3p0.max_size" value="20"/>
<property name="hibernate.c3p0.timeout" value="1800"/>
<property name="hibernate.c3p0.max_statements" value="50"/>
<property name="hibernate.connection.charSet" value="UTF-8"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.id.new_generator_mappings" value="true"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
</properties>
这就是我创建 EntityManagerFactory 的方式:
public class App
...
private static EntityManagerFactory entityManagerFactory;
...
public static void setServletContext(ServletContext context) throws IOException
...
entityManagerFactory = Persistence.createEntityManagerFactory("persistence-unit-name");
...
...
public static EntityManager getEntityManager()
return entityManagerFactory.createEntityManager(); //Here Throws the Exception
...
我使用 ServletContextListener 调用此方法
@WebListener
public class ApplicationServletContextListener implements ServletContextListener
@Override
public void contextInitialized(ServletContextEvent sce)
try
App.setServletContext(sce.getServletContext());
catch (Exception ex)
Logger.getLogger(ApplicationServletContextListener.class.getName()).log(Level.SEVERE, null, ex);
...
这就是我尝试创建 EntityManager 的方式
public class DAO
...
protected EntityManager entityManager;
...
public DAO()
entityManager = App.getEntityManager();
...
...
我已尝试查找有关此 JtaPlatformInaccessibleException 来源的更多信息,但我一无所获。我不确定缺少什么。
【问题讨论】:
我正在使用 Hibernate 5.0.1.Final 和 PostgreSQL 驱动程序 9.2-1002-jdbc4 【参考方案1】:只需将属性添加到您的 persistence.xml
<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform"/>
【讨论】:
这是对我有用的解决方案。原始海报的解决方案在我的情况下不起作用。在 Wildfly 10.0.0.Final 上使用 Hibernate 5.0.7,我需要在我的 persistence.xml 中添加以下几行:好的... 这只是持久性单元 xml 配置中的一个错误,我从持久性单元标记中删除了 transaction-type="JTA"。好像是IDE自动添加的。
【讨论】:
根据文档,JTA
是 transaction-type
的默认值。无论您是定义它还是完全忽略它,都应该没有区别。【参考方案3】:
将@Transactional
添加到您的 DAO 和访问您的 DAO 类的类。
参考:http://spring.io/guides/gs/managing-transactions/
【讨论】:
我没有使用 Spring... 只是一个简单的 HttpServlet。我试图添加@Transactional,但什么也没做.. @Transactional 是 JPA javax.transaction.Transactional 的一部分 是的,我知道......我添加了它......但什么也没做。以上是关于Hibernate:无法访问 TransactionManager 或 UserTransaction 以进行物理事务委托的主要内容,如果未能解决你的问题,请参考以下文章
Hibernate:无法访问 TransactionManager 或 UserTransaction 以进行物理事务委托
Hibernate 无法访问 phpMyAdmin 插入的数据