我有一个基本的 ESB Fuse 测试项目,其中包含以下模块:
简单数据源 简单模型 简单服务
数据源通过蓝图配置,数据源附加到jndi:
<?xml version="1.0" encoding="UTF-8"?>
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>
<!--bean id="simpleDataSource" class="oracle.jdbc.driver.OracleDriver">
<property name="URL" value="jdbc:oracle:thin:@(DESCRIPTION=(LOAD_BALANCE=yes)(CONNECT_DATA=(SERVICE_NAME=devd))(ADDRESS=(PROTOCOL=TCP)(HOST=10.75.192.195)(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 引用数据源(注意这里定义的长和短 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:service/javax.sql.DataSource/(osgi.jndi.service.name=jdbc/simpleDataSource)</jta-data-source-->
<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 注释:
package com.model;
导入 javax.persistence.Column;导入 javax.persistence.Entity;导入 javax.persistence.Table;
@Entity @Table(name = "SIMPLE") 公共类 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"?>
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" />
现在,当我将这些模块安装到 fuse OSGi 容器中时,simple-datasource 和 simple-module 似乎都安装正确。列出这些模块给出:
[ 274] [Active ] [ ] [ ] [ 60] Simple Model (1.0.0)
[ 275] [Active ] [Created ] [ ] [ 60] Simple Datasource (1.0.0)
我创建了一个使用注入 DataSource 的测试 jdbc 模块,这证实了 DataSource 工作正常,例如
public class DbExample {
DataSource dataSource;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public void test() throws Exception {
Connection con = dataSource.getConnection();
Statement stmt = null;
DatabaseMetaData dbMeta = con.getMetaData();
....
}
现在,当我尝试启动 simple-service 时,它会进入宽限期状态,并将以下消息打印到日志文件中:
2013-07-02 11:05:33,772 | INFO | e-1.0.0-thread-1 | BlueprintContainerImpl | ? ? | 8 - org.apache.aries.blueprint.core - 1.0.1.fuse-71-047 | Bundle simple-service is waiting for dependencies [(&(&(org.apache.aries.jpa.proxy.factory=true)(osgi.unit.name=simple-service-persistence-unit))(objectClass=javax.persistence.EntityManagerFactory))]
列出模块状态表明它处于宽限期状态:
[ 277] [Active ] [GracePeriod ] [ ] [ 60] Simple Service Bundle (1.0.0)
它最终超时并进入故障状态。
现在我最初的想法是它可能是一个损坏的数据源,但 jdbc 模块证明它工作正常。我随后的想法是 jndi 查找工作不正常,尽管我不确定如何检查。有什么方法可以查看 jndi 注册表吗?欢迎任何其他建议。