Skip to main content

Querying JNDI resource on GlassFish always returns null

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
6 replies [Last post]
theplague
Offline
Joined: 2011-05-13

How you doing guys, I had and old Servlet app running on Tomcat that I was trying to move to GlassFish v3, so I downloaded it, installed and the app kicks nicely at first glance but it's not really operational because my code relies on a database connection pool (JNDI) that I had on Tomcat, I followed the steps to create a connection Pool on GlassFish and on the admin portal it worked great, I was able to ping the database successfully, now I'm missing the code that needs to be changed in order to query that JNDI resource, everything that I've tried so far returned NULL, can you guys help me out?
Thank you
I have the following JDBC Connection Pool setup on GlassFish:

  • microsoft_jtds_pool (Ping worked successfully)

_____________________________________________

I have the following JDBC Resource setup on GlassFish:

  • dataConn (Pool Name: microsoft_jtds_pool)

_____________________________________________

I ran the command line "list-jndi-entries" and GlassFish returned:
asadmin> list-jndi-entries
dataConn: org.glassfish.javaee.services.ResourceProxy
java:global: com.sun.enterprise.naming.impl.TransientContext
UserTransaction: com.sun.enterprise.transaction.TransactionNamingProxy$UserTransactionProxy
com.sun.enterprise.container.common.spi.util.InjectionManager: com.sun.enterprise.container.common.impl.util.InjectionManagerImpl
ejb: com.sun.enterprise.naming.impl.TransientContext
jdbc: com.sun.enterprise.naming.impl.TransientContext
_____________________________________________

I've been trying the following code on a JSP file:
<%
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
envCtx.lookup("dataConn");
%>
_____________________________________________

The result is always:
Exception Details: javax.naming.NameNotFoundException
No object bound to name java:comp/env/dataConn

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
cf126330
Offline
Joined: 2005-03-29

DataSource myDataSource = (DataSource) initialContext.lookup("dataConn");

theplague
Offline
Joined: 2011-05-13

cf126330 wrote:

DataSource myDataSource = (DataSource) initialContext.lookup("dataConn");

Right on the money!
It's working fine now, any experience using BoneCPDataSource? I tried to cast it but without luck (can't cast DataSource to BoneCPDataSource)
Thanks a lot

sm157516
Offline
Joined: 2005-03-28

What is the need to cast it to BoneCPDataSource? A getConnection() can be performed directly on the got datasource.

theplague
Offline
Joined: 2011-05-13

sm157516 wrote:

What is the need to cast it to BoneCPDataSource? A getConnection() can be performed directly on the got datasource.

sm157516 wrote:

What is the need to cast it to BoneCPDataSource? A getConnection() can be performed directly on the got datasource.

Do you consider BoneCPDataSource and DBCP more efficient than the standard DataSource?
With DBCP (BasicDataSource) I had the following code:
(BasicDataSource) envCtx.lookup("jdbc/maindb");

sm157516
Offline
Joined: 2005-03-28

A wrapped datasource object is provided by GlassFish.

sm157516
Offline
Joined: 2005-03-28

You could use the unwrap() method on the datasource object by passing in an interface that the datasource implementation implements. You should be able to get the instance of the class and some non-standard/standard methods of that datasource class.