views:

790

answers:

2

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 shuld 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'vve 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". )

A: 

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

JoseK
Tried, re-checked, does not work.
binary_runner
Hmm, can you run through this tutorial and see if all the steps match.http://wiki.eclipse.org/EclipseLink/Examples/JPA/Tomcat_Web_Tutorial
JoseK
I'll look at the tutorial although I do not use eclipse.
binary_runner
A: 

Did you make that resource available for the application by declaring it in your web.xml?

<resource-ref>
  <description>DB Connection</description>
  <res-ref-name>jdbc/myds</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
</resource-ref>
Pascal Thivent
This won't work for global resource, I've accindentally turned it on and got datasource with empty properties (class = "", url = null).But if you have working setup with locally-defined resource, I'm in. I've tried it at the beginning.
binary_runner
@binary_runner: Didn't pay enough attention, didn't notice the resource was global. And yes, I have a working setup with a locally-defined resource, will post it later (not on this computer).
Pascal Thivent
Thank you. I'm still not getting what's wrong, everything looks OK but the lookups fail :(.
binary_runner