Java EE 6嵌入式glassfish嵌入式德比EJB单元测试

时间:2011-01-24 11:49:55

标签: jndi java-ee-6 glassfish-3 derby glassfish-embedded

在执行单元测试之前,查询是关于部署时使用嵌入式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

2 个答案:

答案 0 :(得分:4)

@Bryan他没有尝试在网络服务器模式下运行Derby,他想在嵌入模式下运行它,所以我不认为你的建议在这种情况下会有所帮助。

他看到的问题是因为他的persistence.xml中定义了一个名为jdbc / _ 的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>

答案 1 :(得分:1)

消息“java.net.ConnectException:在端口1527上连接到服务器localhost时出错”表示Derby网络服务器未启动并正在运行。您需要启动Derby网络服务器才能连接到它。以下是启动服务器的方法:http://db.apache.org/derby/docs/10.6/adminguide/tadmincbdjhhfd.html