You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by renalexster <re...@gmail.com> on 2017/06/09 14:23:49 UTC

EntityManager + Transaction + OSGi

Hi,

I'm having problems with the entityManager when I try to run in OSGI
environment (jboss-fuse-6.3-fabric).
Everything works locally running by camel:run, but in the OSGI container the
transaction is not committed in the database

Here
<https://github.com/renalexster/camel-jpa-idempotent/tree/entityManager-transaction>  
is an example of my route.

=================== CamelRoute ===================
public class CamelRoute extends RouteBuilder {

	@Override
	public void configure() throws Exception {
		from("file:input?noop=true&delay=60000").transacted()
		.unmarshal(new BindyCsvDataFormat(Client.class))
		.split().simple("${body}")
			.log("Processing Client [${body.name}]")
			.bean(MyService.class, "recordLog")
			.log("${body.name} processed");
	}

}
=================== CamelRoute ===================


=================== MyService ===================
@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, Exchange exchange) {
		LOG.info("Inserting client ["+client.getName()+"]");
		client = em.merge(client);
		LOG.info("Inserted client ["+client.getId()+" - "+client.getName()+"]");
		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.persist(log);
		LOG.info("Inserted LogClient ["+log.getId()+"]");
	}
}
=================== MyService ===================

=================== persistence.xml ===================
<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" xmlns="http://java.sun.com/xml/ns/persistence">
    <persistence-unit name="persistenceUnit"
        transaction-type="RESOURCE_LOCAL">
 		
 	<provider>org.hibernate.ejb.HibernatePersistence</provider>
 		
        <class>com.mycompany.model.Client</class>
        <class>com.mycompany.model.LogClient</class>
 		
         <properties> 
            <property name="javax.persistence.jdbc.driver"
value="org.postgresql.Driver" />
            <property name="javax.persistence.jdbc.url"
value="jdbc:postgresql://192.168.238.1:5432/camel-jpa" />
            <property name="javax.persistence.jdbc.user" value="camel-jpa"
/>
            <property name="javax.persistence.jdbc.password" value="123456"
/>
            <property name="hibernate.dialect"
value="org.hibernate.dialect.PostgreSQLDialect" /> -->
        </properties>
    </persistence-unit>   
</persistence>
=================== persistence.xml ===================

=================== camel-context.xml ===================
<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"/>
    <ctx:annotation-config/>
    <bean class="com.mycompany.routes.CamelRoute" id="javaCamelRoute"/>
    <bean
        class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
id="jpaAdapter">
        <property name="showSql" 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="dataSource" ref="dataSource"/>
        <property name="jpaVendorAdapter" ref="jpaAdapter"/>
        <property name="persistenceUnitName" value="persistenceUnit"/>
    </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>
=================== camel-context.xml ===================



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

Re: EntityManager + Transaction + OSGi

Posted by renalexster <re...@gmail.com>.
Hi,

The transaction is working, but only when I run the route through maven,
using the camel:run.
If I try to run the same code in Fuse does not work.

2017-06-09 11:51 GMT-03:00 Grzegorz Grzybek [via Camel] <
ml+s465427n5802807h0@n5.nabble.com>:

> If you're running Fuse 6.3, you may find this example[1] useful. I've
> described how datasources work and there are working (docker based) Camel
> routes using XA and non-XA datasources.
>
> Are you sure org.apache.commons.dbcp.BasicDataSource is not working with
> auto-commit mode by default?
>
> regards
> Grzegorz Grzybek
> ===
> [1]:
> https://github.com/FuseByExample/camel-persistence-part2/tree/jboss-
> fuse-6.2
>
> 2017-06-09 16:23 GMT+02:00 renalexster <[hidden email]
> <http:///user/SendEmail.jtp?type=node&node=5802807&i=0>>:
>
> > Hi,
> >
> > I'm having problems with the entityManager when I try to run in OSGI
> > environment (jboss-fuse-6.3-fabric).
> > Everything works locally running by camel:run, but in the OSGI container
> > the
> > transaction is not committed in the database
> >
> > Here
> > <https://github.com/renalexster/camel-jpa-idempotent/tree/entityManager-
> > transaction>
> > is an example of my route.
> >
> > =================== CamelRoute ===================
> > public class CamelRoute extends RouteBuilder {
> >
> >         @Override
> >         public void configure() throws Exception {
> >                 from("file:input?noop=true&delay=60000").transacted()
> >                 .unmarshal(new BindyCsvDataFormat(Client.class))
> >                 .split().simple("${body}")
> >                         .log("Processing Client [${body.name}]")
> >                         .bean(MyService.class, "recordLog")
> >                         .log("${body.name} processed");
> >         }
> >
> > }
> > =================== CamelRoute ===================
> >
> >
> > =================== MyService ===================
> > @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, Exchange exchange) {
> >                 LOG.info("Inserting client ["+client.getName()+"]");
> >                 client = em.merge(client);
> >                 LOG.info("Inserted client ["+client.getId()+" -
> > "+client.getName()+"]");
> >                 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.persist(log);
> >                 LOG.info("Inserted LogClient ["+log.getId()+"]");
> >         }
> > }
> > =================== MyService ===================
> >
> > =================== persistence.xml ===================
> > <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" xmlns="http://java.sun.com/xml/ns/persistence">
> >     <persistence-unit name="persistenceUnit"
> >         transaction-type="RESOURCE_LOCAL">
> >
> >         <provider>org.hibernate.ejb.HibernatePersistence</provider>
> >
> >         <class>com.mycompany.model.Client</class>
> >         <class>com.mycompany.model.LogClient</class>
> >
> >          <properties>
> >             <property name="javax.persistence.jdbc.driver"
> > value="org.postgresql.Driver" />
> >             <property name="javax.persistence.jdbc.url"
> > value="jdbc:postgresql://192.168.238.1:5432/camel-jpa" />
> >             <property name="javax.persistence.jdbc.user"
> value="camel-jpa"
> > />
> >             <property name="javax.persistence.jdbc.password"
> > value="123456"
> > />
> >             <property name="hibernate.dialect"
> > value="org.hibernate.dialect.PostgreSQLDialect" /> -->
> >         </properties>
> >     </persistence-unit>
> > </persistence>
> > =================== persistence.xml ===================
> >
> > =================== camel-context.xml ===================
> > <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"/>
> >     <ctx:annotation-config/>
> >     <bean class="com.mycompany.routes.CamelRoute" id="javaCamelRoute"/>
> >     <bean
> >         class="org.springframework.orm.jpa.vendor.
> > HibernateJpaVendorAdapter"
> > id="jpaAdapter">
> >         <property name="showSql" 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="dataSource" ref="dataSource"/>
> >         <property name="jpaVendorAdapter" ref="jpaAdapter"/>
> >         <property name="persistenceUnitName" value="persistenceUnit"/>
> >     </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>
> > =================== camel-context.xml ===================
> >
> >
> >
> > --
> > View this message in context: http://camel.465427.n5.nabble.
> > com/EntityManager-Transaction-OSGi-tp5802805.html
> > Sent from the Camel - Users mailing list archive at Nabble.com.
> >
>
>
> ------------------------------
> If you reply to this email, your message will be added to the discussion
> below:
> http://camel.465427.n5.nabble.com/EntityManager-Transaction-
> OSGi-tp5802805p5802807.html
> To unsubscribe from EntityManager + Transaction + OSGi, click here
> <http://camel.465427.n5.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=5802805&code=cmVuYWxleHN0ZXJAZ21haWwuY29tfDU4MDI4MDV8LTY2MTczNjMxNg==>
> .
> NAML
> <http://camel.465427.n5.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>




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

Re: EntityManager + Transaction + OSGi

Posted by Grzegorz Grzybek <gr...@gmail.com>.
If you're running Fuse 6.3, you may find this example[1] useful. I've
described how datasources work and there are working (docker based) Camel
routes using XA and non-XA datasources.

Are you sure org.apache.commons.dbcp.BasicDataSource is not working with
auto-commit mode by default?

regards
Grzegorz Grzybek
===
[1]:
https://github.com/FuseByExample/camel-persistence-part2/tree/jboss-fuse-6.2

2017-06-09 16:23 GMT+02:00 renalexster <re...@gmail.com>:

> Hi,
>
> I'm having problems with the entityManager when I try to run in OSGI
> environment (jboss-fuse-6.3-fabric).
> Everything works locally running by camel:run, but in the OSGI container
> the
> transaction is not committed in the database
>
> Here
> <https://github.com/renalexster/camel-jpa-idempotent/tree/entityManager-
> transaction>
> is an example of my route.
>
> =================== CamelRoute ===================
> public class CamelRoute extends RouteBuilder {
>
>         @Override
>         public void configure() throws Exception {
>                 from("file:input?noop=true&delay=60000").transacted()
>                 .unmarshal(new BindyCsvDataFormat(Client.class))
>                 .split().simple("${body}")
>                         .log("Processing Client [${body.name}]")
>                         .bean(MyService.class, "recordLog")
>                         .log("${body.name} processed");
>         }
>
> }
> =================== CamelRoute ===================
>
>
> =================== MyService ===================
> @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, Exchange exchange) {
>                 LOG.info("Inserting client ["+client.getName()+"]");
>                 client = em.merge(client);
>                 LOG.info("Inserted client ["+client.getId()+" -
> "+client.getName()+"]");
>                 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.persist(log);
>                 LOG.info("Inserted LogClient ["+log.getId()+"]");
>         }
> }
> =================== MyService ===================
>
> =================== persistence.xml ===================
> <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" xmlns="http://java.sun.com/xml/ns/persistence">
>     <persistence-unit name="persistenceUnit"
>         transaction-type="RESOURCE_LOCAL">
>
>         <provider>org.hibernate.ejb.HibernatePersistence</provider>
>
>         <class>com.mycompany.model.Client</class>
>         <class>com.mycompany.model.LogClient</class>
>
>          <properties>
>             <property name="javax.persistence.jdbc.driver"
> value="org.postgresql.Driver" />
>             <property name="javax.persistence.jdbc.url"
> value="jdbc:postgresql://192.168.238.1:5432/camel-jpa" />
>             <property name="javax.persistence.jdbc.user" value="camel-jpa"
> />
>             <property name="javax.persistence.jdbc.password"
> value="123456"
> />
>             <property name="hibernate.dialect"
> value="org.hibernate.dialect.PostgreSQLDialect" /> -->
>         </properties>
>     </persistence-unit>
> </persistence>
> =================== persistence.xml ===================
>
> =================== camel-context.xml ===================
> <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"/>
>     <ctx:annotation-config/>
>     <bean class="com.mycompany.routes.CamelRoute" id="javaCamelRoute"/>
>     <bean
>         class="org.springframework.orm.jpa.vendor.
> HibernateJpaVendorAdapter"
> id="jpaAdapter">
>         <property name="showSql" 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="dataSource" ref="dataSource"/>
>         <property name="jpaVendorAdapter" ref="jpaAdapter"/>
>         <property name="persistenceUnitName" value="persistenceUnit"/>
>     </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>
> =================== camel-context.xml ===================
>
>
>
> --
> View this message in context: http://camel.465427.n5.nabble.
> com/EntityManager-Transaction-OSGi-tp5802805.html
> Sent from the Camel - Users mailing list archive at Nabble.com.
>