Using JDBC datasources from context.xml in TomEE
Asked Answered
E

3

6

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");
Eighteenth answered 15/10, 2013 at 19:30 Comment(7)
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?Eighteenth
make sure context.xml is placed in META-INF folder.Porridge
Of course that it is in META-INFEighteenth
Did you redeployed your war?Akee
Sure, I'm not a newbieEighteenth
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?Connett
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.Connett
E
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

Eighteenth answered 26/1, 2014 at 3:27 Comment(0)
L
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?

Leontineleontyne answered 15/10, 2013 at 19:33 Comment(4)
And I looked at the examples, but they do with tomee.xml, which is different syntax from context.xmlEighteenth
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. mkyong.com/tomcat/how-to-configure-mysql-datasource-in-tomcat-6Leontineleontyne
Try this Context ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/local_jdbc_db");Leontineleontyne
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");Eighteenth
P
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 :-)

Prodrome answered 25/10, 2020 at 17:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.