已在ApacheCamel中的资源本地EntityManager上调用joinTransaction

时间:2014-02-27 20:30:54

标签: java jpa eclipselink apache-camel integration-patterns

我是apache camel的新手,我正在测试camel-jpa从表中显示和显示记录

以下是主要课程

EntityManagerFactory    entityManagerFactory =  Persistence.createEntityManagerFactory("LoanServicePU");        
CamelContext camelContext = new DefaultCamelContext();

JpaComponent jpa = new JpaComponent();
jpa.setEntityManagerFactory(entityManagerFactory);
JpaTransactionManager myTM=new  JpaTransactionManager();
myTM.setEntityManagerFactory(entityManagerFactory);
jpa.setTransactionManager( myTM );
jpa.setCamelContext(camelContext);
camelContext.addRoutes(new JpaRouteBuilder());
camelContext.addComponent("jpa",jpa);
camelContext.start();
Thread.sleep(10000);
camelContext.stop();
System.out.println("Done");

以下是jparouter类

public void configure() throws Exception {
          from("jpa://com.pns.ab.model.LoanRequest?consumeDelete=false;"
                + "consumer.delay=2000;maxMessagesPerPoll=1000;"
                + "consumer.namedQuery=selectLoanRequests").to("stream:out");
}

我在META-INF下配置了persistence.xml,实际上在eclipse中我启动Java Project然后设置JPA facet

的persistence.xml

<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="LoanServicePU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>com.pns.ab.model.LoanRequest</class>
       <properties>
       <property name="eclipselink.target-server" value="None"/>
       <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
       <property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@127.0.0.1:1521:xe"/>
       <property name="javax.persistence.jdbc.user" value="vs"/>
       <property name="javax.persistence.jdbc.password" value="vs"/>
       <property name="eclipselink.logging.level" value="INFO"/>
      </properties>
  </persistence-unit>
</persistence>

但我收到以下错误:

[main] INFO org.apache.camel.impl.DefaultCamelContext - Apache Camel 2.12.3 (CamelContext: camel-1) started in 1.426 seconds
[Camel (camel-1) thread #0 - jpa://com.pns.ab.model.LoanRequest] WARN org.apache.camel.component.jpa.JpaConsumer - Consumer Consumer[jpa://com.pns.ab.model.LoanRequest?consumeDelete=false%3Bconsumer.delay%3D2000&consumer.namedQuery=selectLoanRequests] failed polling endpoint: Endpoint[jpa://com.pns.ab.model.LoanRequest?consumeDelete=false%3Bconsumer.delay%3D2000&consumer.namedQuery=selectLoanRequests]. Will try again at next poll. Caused by: [javax.persistence.TransactionRequiredException - joinTransaction has been called on a resource-local EntityManager which is unable to register for a JTA transaction.]
javax.persistence.TransactionRequiredException: joinTransaction has been called on a resource-local EntityManager which is unable to register for a JTA transaction.
at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionWrapper.registerIfRequired(EntityTransactionWrapper.java:91)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.joinTransaction(EntityManagerImpl.java:2081)

1 个答案:

答案 0 :(得分:0)

从日志中

  

资源本地EntityManager,无法注册JTA事务

我的结论是将驼峰路由部署到JTA事务环境,但在persistence.xml中,您可以使用transaction-type而不是RESOURCE_LOCAL的默认JTA

修改

通过以下设置,我可以使其工作:

  1. 请勿自行初始化EntityManagerFactoryTransactionManager,只需执行以下操作:

    final SimpleRegistry registry = new SimpleRegistry();
    final CamelContext context = new DefaultCamelContext(registry);
    context.addRoutes(new JpaSetupRouteBuilder());
    context.start();
    
  2. persistence.xml中将您的persistence-unit重命名为camel,例如:

    <!-- setting the transaction-type to RESOURCE_LOCAL is optional as this is the default -->
    <persistence-unit name="camel" transaction-type="RESOURCE_LOCAL"> 
    
  3. 是的,我知道,这不是很令人满意。

    修改

    如果您不想或无法将persistence-unit重命名为camel,则可以使用persistenceUnit选项在URI中设置其名称,例如:

    from("jpa://com.pns.ab.model.LoanRequest?consumeDelete=false"
             + "&consumer.delay=2000;maxMessagesPerPoll=1000"
             + "&consumer.namedQuery=selectLoanRequests"
             + "&persistenceUnit=LoanServicePU")
        .to("stream:out");
    

    修改

    或者,使用如here所述的Spring XML设置。