camel-jpa "no transaction is in progress"

1.2k views Asked by At

i am trying to write an entity to database with camel jpa component. The problem is that i get

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)

The persistent unit is parsed fin and the table is created. Here 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>

I created a test to write one entity:

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

}

Since there is no real way to start and end a transaction in camel semantic i think the problem is related to setup?

So how to fix this?

EDIT

I followed claus' advice and added .transacted() to the route. This yields No bean could be found in the registry of type: PlatformTransactionManager Then i added this line to setup method:

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

and this again yields javax.persistence.TransactionRequiredException: no transaction is in progress.

i am not sure what to configure on JpaComponent and what to bind into the registry.

Meanwhile i debugged a bit. I checked the fields in JpaProducer where entityManager.flush() is throwing the exception: trasactionTemplate is set "PRPAGATION_REQUIRED,ISOLATION_DEFAULT" with correct JpaTransactionManager.

2

There are 2 answers

0
Sagar On

Try using REQUIRES_NEW Transaction Policy to initiate Transaction

[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
Sagar On

you can create the bean instance same way in setup ,

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

and add it to context