camel-jpa"没有正在进行的交易"

时间:2016-09-19 12:06:12

标签: hibernate jpa apache-camel camel-jpa

我正在尝试使用camel jpa组件将实体写入数据库。问题是我得到了

javax.persistence.TransactionRequiredException: no transaction is in progress
    at org.hibernate.internal.SessionImpl.checkTransactionNeeded(SessionImpl.java:3428)
    at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1395)
    at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1391)
    at org.apache.camel.component.jpa.JpaProducer$1.doInTransaction(JpaProducer.java:85)

持久化单元被解析为fin并创建表。这里是persistence.xml:

<persistence 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_2_0.xsd"
             version="2.0">
    <persistence-unit name="pu" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <properties>
            <property name="hibernate.archive.autodetection" value="class" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
            <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver" />
            <property name="hibernate.connection.url" value="jdbc:oracle:thin:@localhost:1521:xe" />
            <property name="hibernate.connection.username" value="user" />
            <property name="hibernate.connection.password" value="pass" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
        </properties>
    </persistence-unit>
</persistence>

我创建了一个测试来编写一个实体:

/**
 * Created by moritz on 16.09.2016.
 */
public class JpaTest extends CamelTestSupport {

    @EndpointInject(uri = "mock:out")
    protected MockEndpoint out;

    @EndpointInject(uri = "direct:in")
    protected DirectEndpoint in;

    @EndpointInject()
    @Test
    public void writeSome() throws Exception {
        PartnerFactaEntity e = new PartnerFactaEntity();
        e.setNameIrs("huhu");
        e.setPartnerId("1");
        e.setUsDokumentGueltigBis(new Date());
        e.setUsDokumentGueltigVon(new Date());
        context.start();

        ProducerTemplate template = context.createProducerTemplate();

        template.sendBody(in, e);

    }

    @Before
    public void setup() throws Exception {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("pu");
        // ( (JndiRegistry) context.getRegistry() ).bind(
        // "EntityManagerFactory", emf );
        // ( (JndiRegistry) context.getRegistry() ).bind( "TransactionManager",
        // new JpaTransactionManager() );
        JpaComponent jpaComponent = context.getComponent("jpa", JpaComponent.class);
        jpaComponent.setEntityManagerFactory(emf);
         JpaTransactionManager taM = new JpaTransactionManager();
         taM.setEntityManagerFactory( emf );
         jpaComponent.setTransactionManager( taM );
        context.addRoutes(new TestRoute());

    }

    @Override
    public boolean isUseRouteBuilder() {
        return false;
    }

    private class TestRoute extends RouteBuilder {

        @Override
        public void configure() throws Exception {
            from(in).to("jpa:PartnerFactaEntity").to(out);
        }
    }

}

由于在驼峰语义中没有真正的方法来启动和结束事务,我认为问题与设置有关吗?

那么如何解决这个问题?

修改

我跟着克劳斯&#39;建议并将.transacted()添加到路线中。这会产生No bean could be found in the registry of type: PlatformTransactionManager 然后我将此行添加到设置方法:

((JndiRegistry)( (PropertyPlaceholderDelegateRegistry) context.getRegistry()).getRegistry()).bind( "PlatformTransactionManager", taM  );

再次产生javax.persistence.TransactionRequiredException: no transaction is in progress

我不确定在JpaComponent上配置什么以及绑定到注册表中的内容。

与此同时我调试了一下。我检查了JpaProducer中entityManager.flush()抛出异常的字段: trasactionTemplate已设置&#34; PRPAGATION_REQUIRED,ISOLATION_DEFAULT&#34;使用正确的JpaTransactionManager。

2 个答案:

答案 0 :(得分:0)

尝试使用REQUIRES_NEW交易政策启动交易

[Google drive Public

<bean id="PROPAGATION_REQUIRES_NEW" class="org.apache.camel.spring.spi.SpringTransactionPolicy">
  <property name="transactionManager" ref="jmsTransactionManager"/>
  <property name="propagationBehaviorName" value="PROPAGATION_REQUIRES_NEW"/>
</bean>

.transacted("PROPAGATION_REQUIRES_NEW")

答案 1 :(得分:0)

您可以在setup中以相同的方式创建bean实例,

    SpringTransactionPolicy policy = new SpringTransactionPolicy ();
    policy = policy.setTransactionManager(taM);
    policy.setPropagationBehav‌​iourName("PROPAGATIO‌​N_REQUIRES_NEW");

并将其添加到上下文