5

TomEE is a great project, combining the lightweight experience of Tomcat with Java EE features. I have many JDBC datasources declared in context.xml, but when I want to use that Datasource via JNDI I get an Exception. So how can I get working a JDBC datasource declared in context.xml in TomEE

My datasource declared in context.xml

 <Resource auth="Container" 
        name="local_jdbc_db"  
        type="javax.sql.DataSource" 
        driverClassName="com.mysql.jdbc.Driver"  
        url="jdbc:mysql://localhost:3306/mydb" 
        username="user" 
        password="pass"      /> 

The code to get the Datasource from JNDI

Context contextoInicial = new InitialContext();
Context contexto = (Context) contextoInicial.lookup("java:comp/env");
DataSource ds= (DataSource) contexto.lookup("local_jdbc_db");
Nestor Hernandez Loli
  • 1,412
  • 3
  • 21
  • 26
  • In TomEE website they say 'Any Tomcat provided resources, say from a context.xml, can be looked up or injected by any managed component in the system.' Is it totally true? – Nestor Hernandez Loli Oct 15 '13 at 19:41
  • make sure `context.xml` is placed in `META-INF` folder. –  Oct 15 '13 at 19:55
  • Of course that it is in META-INF – Nestor Hernandez Loli Oct 15 '13 at 20:04
  • Did you redeployed your war? – MGorgon Oct 15 '13 at 20:47
  • 1
    Sure, I'm not a newbie – Nestor Hernandez Loli Oct 15 '13 at 20:50
  • Nestor, did you ever get this working? I'm in the same situation trying to migrate from Tomcat to TomEE, but it doesn't seem to read the datasources configured in the context.xml. Did you have any luck with tomee.xml? – asgs Nov 27 '13 at 05:14
  • 1
    Actually, it was a false positive. Having the `context.xml` in the webapp's `META-INF/` folder fixed it. I was placing it in a different folder (maven generated) which is `src/main/resources/META-INF`. The correct location would be `src/main/webapp/META-INF`. – asgs Nov 27 '13 at 05:56

3 Answers3

4

UPDATE Just to anyone having this rare problems with TomEE: I tried creating datasources in context.xml and deploying in TomEE JAX-RS version 1.5.0 with no luck, it always throws me null pointer exceptions and datasource name not found. Recently I tried the same with TomEE JAX-RS version 1.6.0: I created my datasource in context.xml and create a simple servlet with this code

     Context initContext = new InitialContext();
     Context envContext = (Context) initContext.lookup("java:/comp/env");
     DataSource dataSource = (DataSource) envContext.lookup("jdbc_northwind");
     try (Connection conn = dataSource.getConnection(); 
           Statement s = conn.createStatement();
           ResultSet rs = s.executeQuery("select * from customers")) {
           while (rs.next()) {
                out.print(rs.getString("CompanyName"));
                out.print("<br>");
           }         
       } 

... started the server and Hooray !!!! It shows me results!, but I was a bit was disappointed because when I redeployed the application it shows me the old exceptions (DataSource not found, null pointer exception) So I tried the last thing: register the datasource in web.xml

  <resource-ref>
        <res-ref-name>jdbc_northwind</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref> 

And restart the server and... It works, redeploy the application and works very fine!, But this behaviour is quite strange: in Tomcat I never declared my datasource in web.xml and I have no problem with datasources. Maybe a bug?

UPDATE: Confirmed is a bug, seems like it will be solved for TomEE 1.6.1 UPDATE 2: Solved in TomEE 1.7.0

Nestor Hernandez Loli
  • 1,412
  • 3
  • 21
  • 26
2

Have you looked at the documentation? http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html Additionally, can you provide the exception that you are receiving?

Joe
  • 389
  • 1
  • 13
  • And I looked at the examples, but they do with tomee.xml, which is different syntax from context.xml – Nestor Hernandez Loli Oct 15 '13 at 19:40
  • This site provides a good example of how to get this whole thing working. I have more questions than answers but I would suggest starting here. http://www.mkyong.com/tomcat/how-to-configure-mysql-datasource-in-tomcat-6/ – Joe Oct 15 '13 at 19:55
  • Try this Context ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/local_jdbc_db"); – Joe Oct 15 '13 at 21:37
  • 1
    Context ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/local_jdbc_db"); is equal to say Context contextoInicial = new InitialContext(); Context contexto = (Context) contextoInicial.lookup("java:comp/env"); DataSource ds= (DataSource) contexto.lookup("local_jdbc_db"); – Nestor Hernandez Loli Oct 15 '13 at 21:42
0

Because I spent so many hours with this problem which I finally found the answer within 15min of reading the manual (RTFM) for this subject at: http://tomee.apache.org/configuring-datasources.html

The proper JNDI namespace for database resources in tomee.xml is: "java:openejb/Resource/..."

Just leaving this here just in case someone else comes looking for this issue.

I cannot emphasize enough how much time I wasted on this when I could have just read the manual. Oh well :-)

Daniel Maldonado
  • 314
  • 4
  • 19