gpt4 book ai didi

jndi - Java EE 6嵌入式Glassfish嵌入式derby EJB单元测试

转载 作者:行者123 更新时间:2023-12-02 09:46:32 27 4
gpt4 key购买 nike

寻求有关在部署单元测试之前在部署时具有嵌入式玻璃鱼和嵌入式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

最佳答案

@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具有以下内容。根据特定应用程序所需的设置,您的外观可能会略有不同。

<jdbc-connection-pool connection-validation-method="auto-commit" validation-table-name="SYS.SYSALIASES" allow-non-component-callers="true" connection-leak-reclaim="true" lazy-connection-association="true" connection-creation-retry-attempts="90" lazy-connection-enlistment="true" validate-atmost-once-period-in-seconds="120" driver-classname="" datasource-classname="org.apache.derby.jdbc.EmbeddedDataSource40" res-type="javax.sql.DataSource" connection-leak-timeout-in-seconds="60" description="" name="GFEmbeddedPool" is-connection-validation-required="true">

<property name="DatabaseName" value="C:\tmp\db\unit-test"></property>
<property name="ConnectionAttributes" value=";create=true"></property>
<property name="AttributesAsPassword" value="false"></property>
<property name="LoginTimeout" value="0"></property>

</jdbc-connection-pool>
<jdbc-resource pool-name="GFEmbeddedPool" description="" jndi-name="jdbc/__embeddedGF"></jdbc-resource>

关于jndi - Java EE 6嵌入式Glassfish嵌入式derby EJB单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4781618/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com