冬眠时,MySQL,GlassFish的第三版和JTA数据源
-
23-09-2019 - |
题
我试图使用Hibernate的实体管理MySQL和GlassFish来。我得到尝试使用JTA数据源时,出现以下错误:
Caused by: org.hibernate.HibernateException: The chosen transaction strategy requires access to the JTA TransactionManager
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:376)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1367)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:858)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:733)
... 37 more
下面是我已经配置我的persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="myPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/mysql</jta-data-source>
<class>com.my.shared.entity.MyFile</class>
<class>com.my.shared.entity.MyRole</class>
<class>com.my.shared.entity.MyUser</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.show.sql" value="true" />
</properties>
然而,当我配置非JTA数据源,它工作正常
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="myPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>jdbc/mysql</non-jta-data-source>
<class>com.my.shared.entity.MyFile</class>
<class>com.my.shared.entity.MyRole</class>
<class>com.my.shared.entity.MyUser</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.show.sql" value="true" />
</properties>
</persistence-unit>
</persistence>
这一切都很好,但我真的想使用:
em.persist(myObject);
的代替:
em.getTransaction().begin();
em.persist(myObject);
em.getTransaction().commit();
我缺少与hibernate配置的东西,或者是它甚至有可能使用一个JTA数据源?
解决方案
这似乎是你的配置,容器管理事务的默认情况下使用。在这种情况下,你需要定义事务同步的方式,使持久层通知(并可以更新例如2级高速缓存)。所以,你需要定义 manager_lookup_class
属性如下:
// For GlassFish:
hibernate.transaction.manager_lookup_class=org.hibernate.transaction.SunONETransactionManagerLookup
// For WebSpere:
hibernate.transaction.manager_lookup_class=org.hibernate.transaction.WebSphereExtendedJTATransactionLookup
// For JBoss:
hibernate.transaction.manager_lookup_class=org.hibernate.transaction.JBossTransactionManagerLookup
// For OpenEJB:
hibernate.transaction.manager_lookup_class=org.apache.openejb.hibernate.TransactionManagerLookup
你也必须标记商业方法,作为“交易”访问数据层。对于您需要将它们与@javax.ejb.TransactionAttribute(REQUIRED)
标志(见这里关于该注解的更多信息)。
您也有一个选项切换到bean管理的事务。你可以这样做:
hibernate.transaction.factory_class=org.hibernate.transaction.JTATransactionFactory
然后,豆负责启动/结束该事务:
org.hibernate.Session session = ...;
org.hibernate.Transaction tx = null;
try {
tx = session.beginTransaction();
session.createQuery(...); // do some staff
tx.commit();
} catch (HibernateException e)
{
if (tx != null) {
tx.rollback();
}
}
不隶属于 StackOverflow