I have been attempting to us JPA to persist an entity in a simple Fuse ESB project, but I'm facing the problem that the entity never gets written to the the underlying database. The project is structured with the following three modules:
simple-datasource
simple-model
simple-service
The datasource is configured through blueprint and the datasource attached to jndi:
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.osgi.org/xmlns/blueprint/v1.0.0
http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">
<bean id="simpleDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@(DESCRIPTION=(LOAD_BALANCE=yes)(CONNECT_DATA=(SERVICE_NAME=xyz))(ADDRESS=(PROTOCOL=TCP)(HOST=xx.xx.xx.xx)(PORT=1521)))" />
<property name="username" value="username" />
<property name="password" value="password" />
</bean>
<service ref="simpleDataSource" interface="javax.sql.DataSource">
<service-properties>
<entry key="osgi.jndi.service.name" value="jdbc/simpleDataSource" />
</service-properties>
</service>
The model defines a persistent unit inside persistence.xml file and references the datasource through jndi:
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
version="2.0">
<persistence-unit name="simple-service-persistence-unit" transaction-type="JTA">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<jta-data-source>osgi.jndi.service.name=jdbc/simpleDataSource</jta-data-source>
<!-- list of the persistance classes -->
<class>com.model.SimpleRow</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
</persistence-unit>
The SimpleRow class uses JPA annotations:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name = "SIMPLE")
public class SimpleRow {
@Column(name = "simple_id")
private Long simpleId;
@Column(name = "simple_text", length =100)
private String simpleText;
public Long getSimpleId() {
return simpleId;
}
public void setSimpleId(Long simpleId) {
this.simpleId = simpleId;
}
public String getSimpleText() {
return simpleText;
}
public void setSimpleText(String simpleText) {
this.simpleText = simpleText;
}
}
I then inject the EntityManager into a service, again using blueprint and a reference to the simple-service-persistence-unit and specify that method level transaction demarcation should be used (as I've seen in all the examples):
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://aries.apache.org/xmlns/jpa/v1.1.0" xmlns:tx="http://aries.apache.org/xmlns/transactions/v1.1.0"
xsi:schemaLocation="
http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
http://aries.apache.org/xmlns/jpa/v1.1.0 http://aries.apache.org/schemas/jpa/jpa_110.xsd">
<bean id="simpleService" class="com.service.SimpleServiceImpl">
<jpa:context property="entityManager" unitname="simple-service-persistence-unit" />
<tx:transaction method="*" value="Required" />
</bean>
<service ref="simpleService" interface="com.service.SimpleService" />
The simple service simply creates an entity and persists it using the EntityManager as follows:
public class SimpleServiceImpl implements SimpleService {
EntityManager entityManager;
public void invokeSimpleService() {
try {
SimpleRow row = new SimpleRow();
row.setSimpleText("Some simple text");
entityManager.persist(row);
System.out.println("Persisted row...");
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
}
} ...
One final configuration which is relevant is the feature set which is as follows:
<feature dependency="true" version="${activemq.version}">activemq-camel</feature>
<feature dependency="true" version="${camel.version}">camel-blueprint</feature>
<feature dependency="true" version="${camel.version}">camel-core</feature>
<feature dependency="true" version="${cxf.version}">cxf</feature>
<feature dependency="true" version="${camel.version}">camel-cxf</feature>
<feature dependency="true" version="${camel.version}">camel-jpa</feature>
<feature dependency="true" version="1.0.1.fuse-71-047">transaction</feature>
<feature dependency="true" version="${jpa.version}">jpa</feature>
<feature dependency="true" version="${jndi.version}">jndi</feature>
<bundle>wrap:mvn:net.sourceforge.serp/serp/1.13.1</bundle>
<bundle>wrap:mvn:oracle/ojdbc/11.2.0.3</bundle>
<bundle>mvn:com.h2database/h2/1.3.167</bundle>
<bundle>mvn:commons-dbcp/commons-dbcp/1.4</bundle>
<bundle>mvn:org.apache.commons/commons-lang3/3.1</bundle>
<bundle>mvn:com.company/simple-datasource/${project.version}</bundle>
<bundle>mvn:com.company/simple-model/${project.version}</bundle>
<bundle>mvn:com.company/simple-service/${project.version}</bundle>
I have verified that the datasource works correctly by injecting it as service into another module which uses it with a straight JDBC connection.
However, when invoke SimpleService.invokeSimpleService, the code executes, no exceptions are thrown but the write is not persisted on the database.
If I add a flush after the persist i.e.
entityManager.persist()
then the following error is thrown:
An error occurred: Can only perform operation while a transaction is active.
If I try and start the transaction explicitly, and remove the tx:transaction annotation on the service bean config, then it fails with the following error:
An error occurred: Transaction management is not available for container managed EntityManagers.
Other information that might be of relevance. The underlying EntityManager implementation is:
org.apache.aries.jpa.container.context.transaction.impl.JTAEntityManager
Also the list of installed aries components is:
[ 7] [Active ] [Created ] [ ] [ 20] Apache Aries Blueprint Core (1.0.1.fuse-71-047)
[ 9] [Active ] [ ] [ ] [ 20] Apache Aries Util (1.0.0)
[ 10] [Active ] [Created ] [ ] [ 20] Apache Aries Blueprint CM (1.0.1.fuse-71-047)
[ 11] [Active ] [ ] [ ] [ 20] Apache Aries Proxy API (1.0.0)
[ 12] [Active ] [ ] [ ] [ 20] Apache Aries Proxy Service (1.0.0)
[ 13] [Active ] [ ] [ ] [ 20] Apache Aries Blueprint API (1.0.1.fuse-71-047)
[ 25] [Active ] [ ] [ ] [ 30] Apache Aries JMX Blueprint API (1.0.1.fuse-71-047)
[ 30] [Active ] [ ] [ ] [ 30] Apache Aries JMX Blueprint Core (1.0.1.fuse-71-047)
[ 33] [Active ] [ ] [ ] [ 30] Apache Aries JMX API (1.0.1.fuse-71-047)
[ 38] [Active ] [ ] [ ] [ 30] Apache Aries JMX Core (1.0.1.fuse-71-047)
[ 75] [Active ] [ ] [ ] [ 60] Aries JPA Container Managed Contexts (1.0.0)
[ 77] [Active ] [Created ] [ ] [ 60] Apache Aries Transaction Enlisting JDBC Datasource (1.0.1.fuse-71-047)
[ 81] [Active ] [ ] [ ] [ 60] Aries JPA Container API (1.0.0)
[ 100] [Active ] [Created ] [ ] [ 60] Apache Aries Transaction Blueprint (1.0.1.fuse-71-047)
[ 118] [Active ] [ ] [ ] [ 60] Apache Aries JNDI API (1.0.0)
[ 125] [Active ] [ ] [ ] [ 60] Aries JPA Container (1.0.0)
[ 139] [Active ] [ ] [ ] [ 60] Apache Aries JNDI Core (1.0.0)
[ 144] [Active ] [ ] [ ] [ 60] Apache Aries JNDI Support for Legacy Runtimes (1.0.0)
[ 146] [Active ] [ ] [ ] [ 60] Apache Aries JNDI RMI Handler (1.0.0)
[ 168] [Active ] [Created ] [ ] [ 60] Aries JPA Container blueprint integration for Aries blueprint (1.0.0)
[ 176] [Active ] [ ] [ ] [ 60] Apache Aries Transaction Manager (1.0.1.fuse-71-047)
[ 177] [Active ] [ ] [ ] [ 60] Apache Aries JNDI URL Handler (1.0.0)
Is there anything obviously wrong with this configuration, which follows most of the examples online?