referring Tomcat JNDI datasource in persistence.xml

Rostislav Matl picture Rostislav Matl · May 26, 2010 · Viewed 57.5k times · Source

in server.xml I've defined global resource (I'm using Tomcat 6):

<GlobalNamingResources>
   <Resource name="jdbc/myds" auth="Container"
             type="javax.sql.DataSource"
             maxActive="10" maxIdle="3" maxWait="10000"
             username="sa"  password=""
             driverClassName="org.h2.Driver"
             url="jdbc:h2:~/.myds/data/db"
   />
</GlobalNamingResources>

I see in catalina.out that this is bound, so I suppose it's OK.

In my web app I have the link to the datasource, I'm not sure it's OK:

<Context>    
 <ResourceLink global='jdbc/myds' name='jdbc/myds' type="javax.sql.Datasource"/>    
</Context>

and in application there is persistence.xml:

<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="oam" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <non-jta-data-source>jdbc/myds</non-jta-data-source>
    <!-- class definitions here, nothing else -->

    <properties>
      <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
    </properties>
  </persistence-unit>
</persistence>

It should be OK, but most probably this or the ResourceLink definition is wrong because I'm getting:

javax.naming.NameNotFoundException: Name jdbc is not bound in this Context

What's wrong and why this does not work?

UPDATE:

I've tried to get the datasource directly:

public class WebAppListener implements ServletContextListener
{
    // ServletContextListener interface - start
    public void contextInitialized(ServletContextEvent sce)
    {
        try
        {
            Context initCtx = new InitialContext();
            Context envCtx = (Context) initCtx.lookup("java:comp/env");
            DataSource ds = (DataSource)
            envCtx.lookup("jdbc/myds");
        }
        catch (NamingException ex)
        {
            System.out.println("!!!! Got NamingException:");
            ex.printStackTrace(System.out);
        }
    }

    public void contextDestroyed(ServletContextEvent sce) { }

}

my web.xml:

  <listener>
    <display-name>Listener</display-name>
    <listener-class>WebAppListener</listener-class>
  </listener>

Still getting the same error although I see the datasource in JMX console when I connect to the Tomcat (Catalina - Datasource - javax.sql.Datasource = "jdbc/myds" : ObjectName = Catalina:type=DataSource,class=javax.sql.DataSource,name="jdbc/myds". )

Answer

JoseK picture JoseK · May 26, 2010

The <non-jta-data-source> in persistence.xml should be

java:comp/env/jdbc/myds

as per the response in http://forums.oracle.com/forums/thread.jspa?messageID=1899677

And also is your db driver in $CATALINA_HOME/lib