Java EE 6 嵌入式 glassfish 嵌入式 derby EJB 单元测试
查询是关于带有嵌入式 glassfish 和嵌入式 derby jndi 的 javaee6 在执行单元测试之前在部署时查找数据源...
请在此处找到 persistence.xml...
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="mymodulePU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/__default</jta-data-source>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:derby:C:/myappDB;create=true" />
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
<property name="eclipselink.ddl-generation.output-mode"
value="database" />
<property name="eclipselink.logging.level" value="ALL" />
<property name="eclipselink.logging.file" value="./target/eclipselink.logs" />
</properties>
</persistence-unit>
</persistence>
请在单元测试时在此处找到服务器控制台日志被执行....
Jan 24, 2011 5:12:44 PM com.sun.enterprise.resource.allocator.LocalTxConnectorAllocator createResource
WARNING: poolmgr.create_resource_error
Jan 24, 2011 5:12:44 PM com.sun.enterprise.connectors.ConnectionManagerImpl internalGetConnection
WARNING: poolmgr.get_connection_failure
Jan 24, 2011 5:12:44 PM com.sun.gjc.spi.base.DataSource getConnection
WARNING: jdbc.exc_get_conn
Jan 24, 2011 5:12:44 PM org.eclipse.persistence.session.file:/C:/DD/WORKSPACES/lean-soa-arch/entities/target/classes/_mymodulePU.ejb
SEVERE:
Local Exception Stack:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: Error in allocating a connection. Cause: Connection could not be allocated because: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused: connect.
Error Code: 0
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:309)
......
.......
......
Caused by: java.sql.SQLException: Error in allocating a connection. Cause: Connection could not be allocated because: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused: connect.
at com.sun.gjc.spi.base.DataSource.getConnection(DataSource.java:112)
at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:126)
... 44 more
Jan 24, 2011 5:12:44 PM org.eclipse.persistence.session.file:/C:/DD/WORKSPACES/lean-soa-arch/entities/target/classes/_mymodulePU.properties
FINEST: End deploying Persistence Unit mymodulePU; session file:/C:/DD/WORKSPACES/lean-soa-arch/entities/target/classes/_mymodulePU; state Deployed; factoryCount 1
questing is about javaee6 with embedded glassfish and embedded derby jndi lookup for data source at the time of deployment before unit test is executed....
Please find the persistence.xml here...
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="mymodulePU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/__default</jta-data-source>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:derby:C:/myappDB;create=true" />
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
<property name="eclipselink.ddl-generation.output-mode"
value="database" />
<property name="eclipselink.logging.level" value="ALL" />
<property name="eclipselink.logging.file" value="./target/eclipselink.logs" />
</properties>
</persistence-unit>
</persistence>
Please find the server console log here when the unit test is executed....
Jan 24, 2011 5:12:44 PM com.sun.enterprise.resource.allocator.LocalTxConnectorAllocator createResource
WARNING: poolmgr.create_resource_error
Jan 24, 2011 5:12:44 PM com.sun.enterprise.connectors.ConnectionManagerImpl internalGetConnection
WARNING: poolmgr.get_connection_failure
Jan 24, 2011 5:12:44 PM com.sun.gjc.spi.base.DataSource getConnection
WARNING: jdbc.exc_get_conn
Jan 24, 2011 5:12:44 PM org.eclipse.persistence.session.file:/C:/DD/WORKSPACES/lean-soa-arch/entities/target/classes/_mymodulePU.ejb
SEVERE:
Local Exception Stack:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLException: Error in allocating a connection. Cause: Connection could not be allocated because: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused: connect.
Error Code: 0
at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:309)
......
.......
......
Caused by: java.sql.SQLException: Error in allocating a connection. Cause: Connection could not be allocated because: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused: connect.
at com.sun.gjc.spi.base.DataSource.getConnection(DataSource.java:112)
at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:126)
... 44 more
Jan 24, 2011 5:12:44 PM org.eclipse.persistence.session.file:/C:/DD/WORKSPACES/lean-soa-arch/entities/target/classes/_mymodulePU.properties
FINEST: End deploying Persistence Unit mymodulePU; session file:/C:/DD/WORKSPACES/lean-soa-arch/entities/target/classes/_mymodulePU; state Deployed; factoryCount 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@Bryan他并不想在网络服务器模式下运行Derby,他想在嵌入式模式下运行它,所以我认为你的建议在这种情况下不会有帮助。
他看到的问题是因为他在 persistence.xml 中定义了一个名为 jdbc/_default 的 JTA 数据源。当嵌入式 glassfish 看到它时,它会尝试在其 JNDI 上下文中查找它,当它这样做时,它会找到它正在使用的 domain.xml 中定义的 jdbc/_default 资源。默认情况下,这是在domain.xml 中定义的基于网络的Derby 资源。
Digambar 需要做的是启动 GF 并创建一个使用嵌入式 derby 的新连接池,并创建一个使用该池用于域的新 jdbc 资源。
我使用 glassfish Web 管理控制台,但如果您知道各种参数的所有命令行开关,也可以使用 asadmin 命令。无论哪种方式,您的domain.xml 都将拥有新资源。完成此操作后,只需更改 persistence.xml,以便 jta-datasource 引用这个新的 jdbc 资源,您就应该完成所有设置。您还可以从 persistence.xml 中删除两个 javax.persistence 属性,因为此时不需要它们(这些设置是在配置新连接池资源时设置的)。
配置嵌入式连接池时,请确保将 ;create=true 设置为 ConnectionAttributes 属性。
创建连接池和 jdbc 资源后,我的 domain.xml 具有以下内容。根据您的特定应用程序所需的设置,您的外观可能会略有不同。
@Bryan He's not trying to run Derby in network server mode, he wants to run it in embedded mode so I don't think your suggestion will help in that case.
The problem he is seeing is because he has a JTA datasource called jdbc/_default defined in his persistence.xml. When embedded glassfish sees this it tries to look it up in its JNDI context and when it does it finds the jdbc/_default resource which is defined in the domain.xml it is using. This by default is a network based Derby resource as defined in the domain.xml.
What Digambar needs to do is to start GF and create a new connection pool which uses embedded derby and create a new jdbc resource which uses that pool for the domain.
I used the glassfish web admin console but you can also use the asadmin command if you know all the command line switches for the various parameters. Either way your domain.xml will then have the new resources. Once you do that simply change your persistence.xml so that the jta-datasource references this new jdbc resource and you should be all set. You can also remove the two javax.persistence properties from your persistence.xml because they are not needed at this point (those settings set when you configure the new connection pool resource).
Make sure to set ;create=true to the ConnectionAttributes property when you configure your embedded connection pool.
After creating the connection pool and jdbc resources my domain.xml had the following. Yours may look slightly different based on the settings you need for your specific application.
消息“java.net.ConnectException:在端口 1527 上连接到服务器本地主机时出错”意味着 Derby 网络服务器未启动并运行。您需要启动 Derby 网络服务器才能连接到它。以下是启动服务器的方法: http://db.apache.org/德比/docs/10.6/adminguide/tadmincbdjhhfd.html
The message "java.net.ConnectException : Error connecting to server localhost on port 1527" means that the Derby network server is not up and running. You need to start the Derby network server in order to connect to it. Here's how to start the server: http://db.apache.org/derby/docs/10.6/adminguide/tadmincbdjhhfd.html