简体   繁体   English

事务和JPA / Persistence.xml

[英]Transactions and JPA / Persistence.xml

I am having problems with transactions using Hibernate's implementation of JPA (I am folowing Camel Tracer example ) 我在使用Hibernate的JPA实现进行交易时遇到问题(我在下面是Camel Tracer示例

I am using Hibernate JPA implementation: 我正在使用Hibernate JPA实现:

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-entitymanager</artifactId>
  <version>4.3.5.Final</version>
</dependency>

If I include a persistence.xml in my META-INF folder, everything works OK: 如果我在META-INF文件夹中包含persistence.xml,一切正常:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             version="1.0">
  <persistence-unit name="tracer" transaction-type="RESOURCE_LOCAL">
    <class>org.apache.camel.processor.interceptor.jpa.JpaTraceEventMessage</class>
    <properties>
      <property name="hibernate.dialect" value="..."/>
      <property name="hibernate.connection.driver_class" value="..."/>
      <property name="hibernate.connection.url" value="..."/>
      <property name="hibernate.hbm2ddl.auto" value="create"/>
      <property name="hibernate.connection.username" value="..." />
      <property name="hibernate.connection.password" value="..."/>
    </properties>
  </persistence-unit>
</persistence>

I want to use a Java way, so I remove the persistence.xml and create the following beans: 我想使用Java方式,所以我删除了persistence.xml并创建了以下bean:

@Configuration
@EnableTransactionManagement
public class AppConfiguration
{
      @Bean public EntityManagerFactory entMngFac()
      {
        EntityManagerFactory emf  = Persistence.createEntityManagerFactory("tracer");
        return emf;
      }
      @Bean public DataSource ds()
      {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("...");
        dataSource.setUrl("...");
        dataSource.setUsername( "..." );
        dataSource.setPassword( "..." );
        return dataSource;
      }
      @Bean public PlatformTransactionManager ptm(EntityManagerFactory emf, DataSource ds)
      {
        JpaTransactionManager jpat = new JpaTransactionManager();
        jpat.setDataSource(ds);
        jpat.setEntityManagerFactory(emf);
        return jpat;
      }
      @Bean public TransactionTemplate tranTemp(PlatformTransactionManager ptm)
      {
        TransactionTemplate tt = new TransactionTemplate();
        tt.setTransactionManager(ptm);
        return tt;
      }
}

After saving, when Camel attempts to flush the JPATracer object it has created, I get the following exception: 保存后,当Camel尝试刷新它创建的JPATracer对象时,我得到以下异常:

javax.persistence.TransactionRequiredException: no transaction is in progress
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.checkTransactionNeeded(AbstractEntityManagerImpl.java:1171)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:1332)
    at org.apache.camel.component.jpa.JpaProducer$1.doInTransaction(JpaProducer.java:86)

Which is a little odd as the "no transaction is in progress" error is coming from the "doInTransaction" method. 这有点奇怪,因为“没有事务正在进行中”错误来自“doInTransaction”方法。

My thoughts are that Camel is starting a transaction, and then Hibernate is trying to flush the object and is unaware of the transaction that Camel has started? 我的想法是Camel正在启动一个事务,然后Hibernate试图刷新对象并且不知道Camel已经启动的事务? So there is some mix of up of transactions somewhere, but I can't figure out where. 所以在某个地方有一些混合的交易,但我无法弄清楚在哪里。

Try with the following EntityManagerFactory configuration (note the packagesToScan call) 尝试使用以下EntityManagerFactory配置(请注意packagesToScan调用)

@Bean
public EntityManagerFactory entityManagerFactory() {
   LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
   em.setDataSource(ds());
   em.setPackagesToScan(new String[] { "org.apache.camel.processor.interceptor.jpa" });
   JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
   em.setJpaVendorAdapter(vendorAdapter);
   return em.getObject();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM