hibernate,mysql,glassfish v3和JTA数据源 [英] hibernate, mysql, glassfish v3, and JTA datasource
问题描述
我试图在mysql和glassfish中使用hibernate实体管理器。尝试使用JTA数据源时出现以下错误:
原因:org.hibernate.HibernateException:所选事务策略需要访问LT中的JTA TransactionManager
。在org.hibernate.impl.SessionFactoryImpl&;在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java(SessionFactoryImpl.java:376)
;。&INIT GT :在org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:733)
在org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory 1367)
(AnnotationConfiguration.java:858)
。 .. 37 more
这是我如何配置我的persistence.xml
<?xml version =1.0encoding =UTF-8?>
< persistence version =1.0xmlns =http://java.sun.com/xml/ns/persistencexmlns:xsi =http://www.w3.org/2001/XMLSchema-实例xsi:schemaLocation =http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd\">
< persistence-unit name =myPUtransaction-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>
<属性>
< property name =hibernate.hbm2ddl.autovalue =create-drop/>
< property name =hibernate.show.sqlvalue =true/>
< / properties>
然而,当我配置非jta数据源时,它工作正常
<?xml version =1.0encoding =UTF- 8\" >?;
< persistence version =1.0xmlns =http://java.sun.com/xml/ns/persistencexmlns:xsi =http://www.w3.org/2001/XMLSchema-实例xsi:schemaLocation =http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd\">
< persistence-unit name =myPUtransaction-type =JTA>
< provider> org.hibernate.ejb.HibernatePersistence< / provider>
<非-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>
<属性>
< property name =hibernate.hbm2ddl.autovalue =create-drop/>
< property name =hibernate.show.sqlvalue =true/>
< / properties>
< / persistence-unit>
< /余辉>
这一切都很好,但我真的很想使用:
em.persist(myObject);
而不是:
em.getTransaction()开头();
em.persist(myObject);
em.getTransaction()。commit();
我是缺少与hibernate配置有关的东西,还是甚至有可能使用JTA数据源?解析方案
对于你的配置来说,容器管理的事务是默认使用的。在这种情况下,您需要定义一种事务同步方式,以便通知持久层(并且可以更新二级缓存)。所以你需要定义 manager_lookup_class
属性,如下所示:
//对于GlassFish:
hibernate.transaction.manager_lookup_class属性= org.hibernate.transaction.SunONETransactionManagerLookup
//对于的WebSpere:
hibernate.transaction.manager_lookup_class属性= org.hibernate.transaction.WebSphereExtendedJTATransactionLookup
//对于JBoss :
hibernate.transaction.manager_lookup_class属性= org.hibernate.transaction.JBossTransactionManagerLookup
//对于OpenEJB的:
hibernate.transaction.manager_lookup_class属性= org.apache.openejb.hibernate.TransactionManagerLookup
您还必须将访问数据层的业务方法标记为事务性。为此,您需要使用 @ javax.ejb.TransactionAttribute(REQUIRED)
标记它们(请参阅 here 以获取更多关于此批注的信息)。
您也可以选择切换到Bean管理的事务。你可以这样做:
hibernate.transaction.factory_class = org.hibernate.transaction.JTATransactionFactory
然后bean负责开始/结束交易:
org .hibernate.Session session = ...;
org.hibernate.Transaction tx = null;
尝试{
tx = session.beginTransaction();
session.createQuery(...); //做一些工作人员
tx.commit();
} catch(HibernateException e)
{
if(tx!= null){
tx.rollback();
}
}
I'm attempting to use hibernate entity manager with mysql and glassfish. I'm getting the following error when attempting to use a JTA datasource:
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
Here is how I have configured my 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>
However, when I configure a non-jta datasource, it works fine
<?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>
That's all well and good, but I would really like to use:
em.persist(myObject);
instead of:
em.getTransaction().begin();
em.persist(myObject);
em.getTransaction().commit();
Am I missing something with the hibernate configuration, or is it even possible to use a JTA datasource?
It seems like for your configuration, container-managed transactions are used by default. In this case you need to define a way of transaction synchronization so the persistence layer is notified (and can update the 2nd level cache for example). So you need to define manager_lookup_class
property as following:
// 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
Also you have to mark business methods that access data layer as "transactional". For that you need to mark them with @javax.ejb.TransactionAttribute(REQUIRED)
(see here for more information about this annotation).
You also have an option to switch to bean-managed transactions. You can do it by saying:
hibernate.transaction.factory_class=org.hibernate.transaction.JTATransactionFactory
Then the bean is responsible for starting/ending the transaction:
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();
}
}
这篇关于hibernate,mysql,glassfish v3和JTA数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!