2013-07-04 106 views
0

我一直在试图给我们JPA坚持在一个简单的保险丝ESB项目的实体,但我面对的是实体永远不会被写入到底层数据库的问题。该项目的结构具有以下三个模块:保险丝ESB JPA实体不坚持

简单数据源

简单模型

简单服务

数据源通过蓝图配置和数据源连接到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到服务,再次使用蓝图和简单的服务的持久性单元的参考,并指定方法级吨ransaction划界应使用(正如我在所有的例子见过):

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

如果我尝试和明确启动事务,并删除TX:交易注解在服务bean配置,然后它失败并出现以下错误:

An error occurred: Transaction management is not available for container managed EntityManagers. 

其他inf这可能是相关的。底层的EntityManager实现:

org.apache.aries.jpa.container.context.transaction.impl.JTAEntityManager 

还安装白羊座的组件列表是:

[ 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

我不确定使用resource_local时的确切时间,因为事务类型在持久层中是适当的。然而,修改的persistence.xml如下使用JTA和JTA数据源固定我的问题:

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

+0

我怀疑的原因是正在使用的不正确的EntityManager。尽管指定了openjpa持久性提供程序,但blueprint最终将注入容器EntityManager:org.apache.aries.jpa.container.context.transaction.impl.JTAEntityManager。这可能是由于openjpa实现类在这种情况下对服务模块不可用,所以默认为容器实现。 – Ellis