hibernate、mysql、glassfish v3 和 JTA 数据源
我正在尝试将 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 数据源吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于您的配置来说,默认情况下似乎使用容器管理的事务。在这种情况下,您需要定义一种事务同步方式,以便通知持久层(例如,可以更新二级缓存)。所以你需要定义
manager_lookup_class
属性如下:此外,您还必须将访问数据层的业务方法标记为“事务性”。为此,您需要用
@javax.ejb.TransactionAttribute(REQUIRED)
标记它们(请参阅 此处 了解有关此注释的更多信息)。您还可以选择切换到 Bean 管理的事务。你可以这样说:
hibernate.transaction.factory_class=org.hibernate.transaction.JTATransactionFactory
然后 bean 负责启动/结束事务:
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: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: