2016-09-19 3 views
0

Ich versuche, eine Entität in die Datenbank mit Kamel Jpa-Komponente zu schreiben. Das Problem ist, dass ich bekommecamel-jpa "keine Transaktion ist in Bearbeitung"

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) 

Die persistente Einheit ist analysiert Flosse und die Tabelle erstellt. Hier ist 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> 

ich einen Test erstellt eine Einheit zu schreiben:

/** 
* 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); 
     } 
    } 

} 

Da es keine wirkliche Möglichkeit ist, eine Transaktion in Kamel semantischen ich denke, zu starten und beenden ist das Problem damit verbundene einrichten?

Also, wie das zu beheben?

EDIT

Ich folgte Rat Claus' und fügte hinzu .transacted() auf die Route. Dies ergibt No bean could be found in the registry of type: PlatformTransactionManager Dann i diese Zeile Setup-Methode hinzugefügt:

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

und dies wiederum führt zu javax.persistence.TransactionRequiredException: no transaction is in progress.

Ich bin mir nicht sicher, was auf JpaComponent zu konfigurieren und was in die Registrierung zu binden.

Inzwischen debuggte ich ein bisschen. Ich überprüfte die Felder in JpaProducer, wo entityManager.flush() die Ausnahme wirft: trasactionTemplate ist gesetzt "PRPAGATION_REQUIRED, ISOLATION_DEFAULT" mit korrekten JpaTransactionManager.

+0

Es ist transacted() müssen Sie in Ihrem Kamel verwenden, um Camel zu sagen a Transaktion. Siehe die Dokumentation unter: http://camel.apache.org/transactional-client.html –

+0

Ich habe es versucht, aber es hat nicht geholfen - siehe meine Bearbeitung – dermoritz

Antwort

0

Versuchen REQUIRES_NEW Transaktion Politik mit der Transaktion

[https://camel.apache.org/transactional-client.html][1]

<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") 
+0

danke, gibt es eine Möglichkeit, dies in Java zu tun? – dermoritz

0

Sie die Bean-Instanz gleiche Art und Weise in Setup, um Kontext

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

und fügen Sie es schaffen können initiieren

Verwandte Themen