Hi,

In my actual application, i have a business layer that uses JPA to persist
data according to some business rules, the problem is that the camel-jpa
transaction is not shared with the business layer transaction. I need my
EntityManager in business classes to be integrated with the Camel
transaction scope, how can I do this?


Below is a simple example, but that reflects the problem in the actual
design.


Project example
<https://github.com/renalexster/camel-jpa-idempotent/tree/lock_transaction>  


==============================
@Component
public class MyService {
        @PersistenceContext(unitName="persistenceUnit") private EntityManager 
em;
        
        private static final Logger LOG = 
LoggerFactory.getLogger(MyService.class);
        
        public void recordLog(@Body Client client) {
                LOG.info("Inserting LogClient");
                LogClient log = new LogClient();
                log.setClient(client);
                SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy 
HH:mm:ss");
                String strDate = sdf.format(new Date());
                log.setTxtLog("Persisted Client ["+client.getName()+"] at 
["+strDate+"]");
                
                em.merge(log);
                LOG.info("Inserted LogClient ["+log.getId()+"]");
                
                
        }
==============================


==============================
@Component
public class CamelRoute extends RouteBuilder {
        
        @Override
        public void configure() throws Exception {
        
from("jpa:com.mycompany.model.Client?persistenceUnit=persistenceUnit&consumeDelete=false&consumer.delay=15000").transacted()
                .split().simple("${body}")
                        .log("Processing Client [${body.name}]")
                        .bean(MyService.class, "recordLog")
                        .log("${body.name} processed");
        }
}
==============================



==============================
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans";
    xmlns:camel="http://camel.apache.org/schema/spring";
    xmlns:ctx="http://www.springframework.org/schema/context";
    xmlns:osgi="http://www.springframework.org/schema/osgi";
    xmlns:osgix="http://www.springframework.org/schema/osgi-compendium";
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
xsi:schemaLocation="http://www.springframework.org/schema/beans                 
http://www.springframework.org/schema/beans/spring-beans.xsd                 
http://www.springframework.org/schema/osgi     
http://www.springframework.org/schema/osgi/spring-osgi.xsd    
http://www.springframework.org/schema/osgi-compendium     
http://www.springframework.org/schema/osgi-compendium/spring-osgi-compendium.xsd
    
http://www.springframework.org/schema/context                 
http://www.springframework.org/schema/context/spring-context-3.0.xsd            
     
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring.xsd";>
    <osgix:cm-properties id="parametros.spring"
persistent-id="parametros.spring">
        <prop key="db.driverClassName">org.postgresql.Driver</prop>
        <prop
key="db.url">jdbc:postgresql://192.168.238.1:5432/camel-jpa</prop>
        <prop key="db.username">camel-jpa</prop>
        <prop key="db.password">123456</prop>
        <prop key="connection.show_sql">true</prop>
    </osgix:cm-properties>
    <ctx:property-placeholder properties-ref="parametros.spring"/>
    <bean class="com.mycompany.routes.CamelRoute" id="javaCamelRoute"/>
    <ctx:annotation-config/>
    <bean
        class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
id="jpaAdapter">
        <property name="showSql" value="${connection.show_sql}"/>
        <property name="generateDdl" value="true"/>
        <property name="databasePlatform"
value="org.hibernate.dialect.PostgreSQLDialect"/>
    </bean>
    <bean class="org.apache.commons.dbcp.BasicDataSource" id="dataSource">
        <property name="driverClassName" value="${db.driverClassName}"/>
        <property name="url" value="${db.url}"/>
        <property name="username" value="${db.username}"/>
        <property name="password" value="${db.password}"/>
    </bean>
    <bean class="org.apache.camel.component.jpa.JpaComponent" id="jpa">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
        <property name="transactionManager" ref="jpaTxManager"/>
    </bean>
    <bean class="org.springframework.orm.jpa.JpaTransactionManager"
id="jpaTxManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>
    <bean
       
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
id="entityManagerFactory">
        <property name="persistenceUnitName" value="persistenceUnit"/>
        <property name="dataSource" ref="dataSource"/>
        <property name="jpaVendorAdapter" ref="jpaAdapter"/>
    </bean>
    <camelContext id="amq-example-context"
        xmlns="http://camel.apache.org/schema/spring";
xmlns:order="http://com.mycompany/examples/order";>
        <propertyPlaceholder id="properties"
location="ref:parametros.spring"/>
        <routeBuilder ref="javaCamelRoute"/>
    </camelContext>
</beans>
==============================



--
View this message in context: 
http://camel.465427.n5.nabble.com/Camel-JPA-Transaction-EntityManager-tp5802279.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Reply via email to