I've looked this up enough times, so I thought I'd jott this down. This is how I look up my JNDI resource from Java:
private final mJNDIResourceName = "my_data_source"; [..] InitialContext context = new InitialContext(); Context envContext = (Context) context.lookup("java:comp/env"); DataSource dataSource = (DataSource) envContext.lookup(mJNDIResourceName); connection = dataSource.getConnection(); connection.setAutoCommit(false);
Most examples on the internet use a
jdbc/my_data_source. This is just some kind
of convention, you don'd need to have
the name. I've removed it from my code because I find it
totally redundant and without any added value.
I've previously described how you configure DB pooling in Tomcat, which also is a JNDI resource. Hence, if you're using Tomcat, you can use this article as a reference for setting it up.