我一直试图让JPA在一个简单的Fuse ESB项目中持久保存一个实体,但我面临的问题是该实体永远不会被写入底层数据库。该项目由以下三个模块构成:
简单数据源
简单的模型
简单服务
数据源通过蓝图配置,数据源附加到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>
模型在persistence.xml文件中定义了一个持久单元,并通过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>
SimpleRow类使用JPA注释:
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;
}
}
然后我将EntityManager注入服务,再次使用蓝图和对simple-service-persistence-unit的引用,并指定应该使用方法级事务划分(正如我在所有示例中看到的):< / p>
<?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" />
简单服务只是创建一个实体并使用EntityManager将其持久化,如下所示:
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());
}
} ...
一个相关的最终配置是功能集,如下所示:
<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>
我已经通过将其作为服务注入另一个使用直接JDBC连接的模块来验证数据源是否正常工作。
但是,在调用SimpleService.invokeSimpleService时,代码会执行,不会抛出任何异常,但写入不会保留在数据库中。
如果我在坚持之后添加一个冲洗,即
entityManager.persist()
然后抛出以下错误:
An error occurred: Can only perform operation while a transaction is active.
如果我尝试显式启动事务,并删除服务bean配置上的tx:transaction批注,则失败并显示以下错误:
An error occurred: Transaction management is not available for container managed EntityManagers.
其他可能相关的信息。底层的EntityManager实现是:
org.apache.aries.jpa.container.context.transaction.impl.JTAEntityManager
此外,已安装的aries组件列表为:
[ 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)
此配置是否存在明显错误,大多数在线示例都是如此?
答案 0 :(得分:0)
我不确定何时使用resource_local,因为事务类型在持久层中是合适的。但是,修改persistence.xml以使用JTA和jta-data-source解决了我的问题:
<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>
<!--non-jta-data-source>osgi:service/jdbc/simpleDataSource</non-jta-data-source-->
<jta-data-source>osgi:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/simpleDataSource)</jta-data-source>
<class>com.company.model.SimpleRow</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="openjpa.Log" value="DefaultLevel=INFO, Tool=INFO, SQL=TRACE"/>
</properties>
</persistence-unit>