0

我一直在尝试让我们 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 的引用,并指定应该使用方法级别的事务划分(正如我在所有示例中看到的那样):

<?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)

这个配置有什么明显的问题吗,它遵循大多数在线示例?

4

1 回答 1

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>

于 2013-07-04T11:21:41.647 回答