Java EE 6嵌入式Glassfish嵌入式derby EJB单元测试 [英] Java EE 6 embedded glassfish embedded derby EJB unit test

查看:84
本文介绍了Java EE 6嵌入式Glassfish嵌入式derby EJB单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关的问题是在部署单元测试之前,在部署时有关具有嵌入式glassfish和嵌入式derby jndi查找数据源的javaee6的信息。

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

请找到persistence.xml此处...

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


推荐答案

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

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

他看到的问题是因为他拥有在他的persistence.xml中定义的JTA数据源jdbc / _ default。当嵌入式glassfish看到此消息时,它将尝试在其JNDI上下文中查找它,当找到它时,它将找到所使用的domain.xml中定义的jdbc / _default资源。默认情况下,这是domain.xml中定义的基于网络的Derby资源。

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.

Digambar需要做的是启动GF并创建一个使用嵌入式derby的新连接池,并创建一个使用该池用于域的新jdbc资源。

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.

我使用了glassfish Web管理控制台,但是如果您知道各种参数的所有命令行开关,也可以使用asadmin命令。无论哪种方式,您的domain.xml都将拥有新资源。一旦这样做,只需更改您的persistence.xml,以便jta-datasource引用此新的jdbc资源,您就应该准备就绪。您还可以从persistence.xml中删除这两个javax.persistence属性,因为这时不需要它们(配置新连接池资源时设置的那些设置)。

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

在配置嵌入式连接池时,请务必将; create = true设置为ConnectionAttributes属性。

Make sure to set ;create=true to the ConnectionAttributes property when you configure your embedded connection pool.

创建连接池和jdbc资源后,我的domain.xml具有以下内容。根据特定应用程序所需的设置,您的外观可能会略有不同。

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.

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

这篇关于Java EE 6嵌入式Glassfish嵌入式derby EJB单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆