嵌入式ejb-Test-Get数据库

时间:2015-11-15 22:40:57

标签: java java-ee glassfish glassfish-embedded

我有一个用于测试的嵌入式ejb容器。 在客户端(测试)我想使用查找机制获取DataSource。 问题是查找方法总是返回错误:

  

javax.naming.NamingException:查找失败   'java:app / jdbc / templatedb'中   SerialContext [myEnv = {java.naming.factory.initial的= com.sun.enterprise.naming.impl.SerialInitContextFactory,   java.naming.factory.state = com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl,   java.naming.factory.url.pkgs = com.sun.enterprise.naming} [Root   异常是javax.naming.NamingException:调用异常:得到   null ComponentInvocation] at   com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:491)

的persistence.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<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" xmlns="http://java.sun.com/xml/ns/persistence">
    <persistence-unit name="dev" transaction-type="JTA">

        <jta-data-source>java:app/jdbc/templatedb</jta-data-source>

        <class>hu.akoel.template.ejb.entities.User</class>
        <class>hu.akoel.template.ejb.entities.Role</class>
        <class>hu.akoel.template.ejb.entities.RoleFeatureRight</class>

        <properties>
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
            <property name="eclipselink.ddl-generation" value="create-tables" />
            <property name="eclipselink.ddl-generation.output-mode" value="database" />
            <property name="disable-nonportable-jndi-names" value="true" />
        </properties>
    </persistence-unit> 
</persistence>

的glassfish-resources.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>
    <jdbc-connection-pool name="PostgresqlPool"
        res-type="javax.sql.ConnectionPoolDataSource" datasource-classname="org.postgresql.ds.PGConnectionPoolDataSource">

        <property name="TargetServerType" value="any"></property>
        <property name="User" value="tmpuser"></property>
        <property name="BinaryTransfer" value="true"></property>
        <property name="UnknownLength" value="2147483647"></property>
        <property name="DisableColumnSanitiser" value="false"></property>
        <property name="UseSpNego" value="false"></property>
        <property name="SspiServiceClass" value="POSTGRES"></property>
        <property name="ProtocolVersion" value="0"></property>
        <property name="LogLevel" value="0"></property>
        <property name="Url" value="jdbc:postgresql://localhost:5432/templatedb?loglevel=0&amp;prepareThreshold=5&amp;binaryTransfer=true&amp;compatible=9.4&amp;readOnly=false&amp;binaryTransferEnable=&amp;binaryTransferDisable=&amp;unknownLength=2147483647&amp;logUnclosedConnections=false&amp;disableColumnSanitiser=false&amp;tcpKeepAlive=false&amp;loginTimeout=0&amp;connectTimeout=0&amp;socketTimeout=0&amp;receiveBufferSize=-1&amp;sendBufferSize=-1&amp;useSpnego=false&amp;gsslib=auto&amp;sspiServiceClass=POSTGRES&amp;allowEncodingChanges=false&amp;targetServerType=any&amp;loadBalanceHosts=false&amp;hostRecheckSeconds=10"></property>
        <property name="AllowEncodingChanges" value="false"></property>
        <property name="PrepareThreshold" value="5"></property>
        <property name="SocketTimeout" value="0"></property>
        <property name="SendBufferSize" value="-1"></property>
        <property name="HostRecheckSeconds" value="10"></property>
        <property name="Password" value="tmpuser"></property>
        <property name="ReceiveBufferSize" value="-1"></property>
        <property name="LoadBalanceHosts" value="true"></property>
        <property name="ReadOnly" value="false"></property>
        <property name="ConnectTimeout" value="0"></property>
        <property name="LogUnclosedConnections" value="false"></property>
        <property name="DatabaseName" value="templatedb"></property>
        <property name="Compatible" value="9.4"></property>
        <property name="GssLib" value="auto"></property>
        <property name="Ssl" value="false"></property>
        <property name="LoginTimeout" value="0"></property>
        <property name="ServerName" value="localhost"></property>
        <property name="PortNumber" value="5432"></property>
        <property name="TcpKeepAlive" value="false"></property>

    </jdbc-connection-pool>
    <jdbc-resource pool-name="PostgresqlPool"
        jndi-name="jdbc/templatedb">
    </jdbc-resource>
</resources>

MyTest.java:

public class MyTest {

    @Test
    public void testAddNumbers() throws NamingException, EJBeanException{

        EJBContainer container = javax.ejb.embeddable.EJBContainer.createEJBContainer();

        DataSource ds = (DataSource)container.getContext().lookup("java:global/jdbc/templatedb");
        //DataSource ds = (DataSource)container.getContext().lookup("java:app/jdbc/templatedb");
        //DataSource ds = (DataSource)container.getContext().lookup("jdbc/templatedb");

    }
}

我尝试了不同的查找属性,但没有一个工作。 有人有什么想法吗?

0 个答案:

没有答案