title: Embedded ejb-Test-Get database
body: <p>I got an embedded ejb container for test.
  On the client side (test) I'd like to get DataSource using lookup mechanism.
  The problem is that the lookup method always returns with an error: </p>
  
  <blockquote>
    <p>javax.naming.NamingException: Lookup failed for
    'java:app/jdbc/templatedb' in
    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
    exception is javax.naming.NamingException: Invocation exception: Got
    null ComponentInvocation ]    at
    com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:491)</p>
  </blockquote>
  
  <p><strong>persistence.xml:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  &lt;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"&gt;
      &lt;persistence-unit name="dev" transaction-type="JTA"&gt;
  
          &lt;jta-data-source&gt;java:app/jdbc/templatedb&lt;/jta-data-source&gt;
  
          &lt;class&gt;hu.akoel.template.ejb.entities.User&lt;/class&gt;
          &lt;class&gt;hu.akoel.template.ejb.entities.Role&lt;/class&gt;
          &lt;class&gt;hu.akoel.template.ejb.entities.RoleFeatureRight&lt;/class&gt;
  
          &lt;properties&gt;
              &lt;property name="eclipselink.ddl-generation" value="drop-and-create-tables" /&gt;
              &lt;property name="eclipselink.ddl-generation" value="create-tables" /&gt;
              &lt;property name="eclipselink.ddl-generation.output-mode" value="database" /&gt;
              &lt;property name="disable-nonportable-jndi-names" value="true" /&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt; 
  &lt;/persistence&gt;
  </code></pre>
  
  <p><strong>glassfish-resources.xml:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" "http://glassfish.org/dtds/glassfish-resources_1_5.dtd"&gt;
  &lt;resources&gt;
      &lt;jdbc-connection-pool name="PostgresqlPool"
          res-type="javax.sql.ConnectionPoolDataSource" datasource-classname="org.postgresql.ds.PGConnectionPoolDataSource"&gt;
  
          &lt;property name="TargetServerType" value="any"&gt;&lt;/property&gt;
          &lt;property name="User" value="tmpuser"&gt;&lt;/property&gt;
          &lt;property name="BinaryTransfer" value="true"&gt;&lt;/property&gt;
          &lt;property name="UnknownLength" value="2147483647"&gt;&lt;/property&gt;
          &lt;property name="DisableColumnSanitiser" value="false"&gt;&lt;/property&gt;
          &lt;property name="UseSpNego" value="false"&gt;&lt;/property&gt;
          &lt;property name="SspiServiceClass" value="POSTGRES"&gt;&lt;/property&gt;
          &lt;property name="ProtocolVersion" value="0"&gt;&lt;/property&gt;
          &lt;property name="LogLevel" value="0"&gt;&lt;/property&gt;
          &lt;property name="Url" value="jdbc:postgresql://localhost:5432/templatedb?loglevel=0&amp;amp;prepareThreshold=5&amp;amp;binaryTransfer=true&amp;amp;compatible=9.4&amp;amp;readOnly=false&amp;amp;binaryTransferEnable=&amp;amp;binaryTransferDisable=&amp;amp;unknownLength=2147483647&amp;amp;logUnclosedConnections=false&amp;amp;disableColumnSanitiser=false&amp;amp;tcpKeepAlive=false&amp;amp;loginTimeout=0&amp;amp;connectTimeout=0&amp;amp;socketTimeout=0&amp;amp;receiveBufferSize=-1&amp;amp;sendBufferSize=-1&amp;amp;useSpnego=false&amp;amp;gsslib=auto&amp;amp;sspiServiceClass=POSTGRES&amp;amp;allowEncodingChanges=false&amp;amp;targetServerType=any&amp;amp;loadBalanceHosts=false&amp;amp;hostRecheckSeconds=10"&gt;&lt;/property&gt;
          &lt;property name="AllowEncodingChanges" value="false"&gt;&lt;/property&gt;
          &lt;property name="PrepareThreshold" value="5"&gt;&lt;/property&gt;
          &lt;property name="SocketTimeout" value="0"&gt;&lt;/property&gt;
          &lt;property name="SendBufferSize" value="-1"&gt;&lt;/property&gt;
          &lt;property name="HostRecheckSeconds" value="10"&gt;&lt;/property&gt;
          &lt;property name="Password" value="tmpuser"&gt;&lt;/property&gt;
          &lt;property name="ReceiveBufferSize" value="-1"&gt;&lt;/property&gt;
          &lt;property name="LoadBalanceHosts" value="true"&gt;&lt;/property&gt;
          &lt;property name="ReadOnly" value="false"&gt;&lt;/property&gt;
          &lt;property name="ConnectTimeout" value="0"&gt;&lt;/property&gt;
          &lt;property name="LogUnclosedConnections" value="false"&gt;&lt;/property&gt;
          &lt;property name="DatabaseName" value="templatedb"&gt;&lt;/property&gt;
          &lt;property name="Compatible" value="9.4"&gt;&lt;/property&gt;
          &lt;property name="GssLib" value="auto"&gt;&lt;/property&gt;
          &lt;property name="Ssl" value="false"&gt;&lt;/property&gt;
          &lt;property name="LoginTimeout" value="0"&gt;&lt;/property&gt;
          &lt;property name="ServerName" value="localhost"&gt;&lt;/property&gt;
          &lt;property name="PortNumber" value="5432"&gt;&lt;/property&gt;
          &lt;property name="TcpKeepAlive" value="false"&gt;&lt;/property&gt;
  
      &lt;/jdbc-connection-pool&gt;
      &lt;jdbc-resource pool-name="PostgresqlPool"
          jndi-name="jdbc/templatedb"&gt;
      &lt;/jdbc-resource&gt;
  &lt;/resources&gt;
  </code></pre>
  
  <p><strong>MyTest.java:</strong></p>
  
  <pre><code>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");
  
      }
  }
  </code></pre>
  
  <p>I tried different lookup attributes but none of them worked.
  Somebody has any idea?</p>