4
我使用Hibernate的JPA實現的(我如下因素Camel Tracer example)事務和JPA/persistence.xml中
我使用Hibernate的JPA實現具有交易問題:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.5.Final</version>
</dependency>
如果我有一個持久性.XML在我的META-INF文件夾,一切正常OK:
<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>
我想使用Java的方式,所以我刪除persistence.xml並創建跟隨着摹豆:
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)
這是因爲「無交易是有點奇怪:
@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;
}
}
保存,當駱駝試圖刷新JPATracer對象已經創建,我得到下面的異常後進度「錯誤來自」doInTransaction「方法。
我的想法是,駱駝啓動一個事務,然後Hibernate試圖刷新對象,並不知道駱駝已經開始的事務?所以在某處有一些交易的組合,但我無法弄清楚在哪裏。
沒錯!那樣做了。不指定要掃描的軟件包會導致「TransactionRequiredException」異常。我必須記住那一個。再次感謝! – kwh
歡迎你;我也喜歡這個網站 –