1
votes

In Glassfish admin, I setup a JDBC connection pool that I can ping ok. Also in Glassfish admin, I have setup a JNDI JDBC resource called jdbc/myDb

However I'm unable to connect to it from Java. I have tried both injection style:

@Resource (mappedName="jdbc/myDB")
private DataSource datasource;

I have also tried Context style :

Context ctx = new InitialContext();
DataSource datasource = (DataSource)ctx.lookup("jdbc/myDB")

All I get is java.lang.NullPointerException.

I have also tried various naming styles such as java:jdbc/myDB or java:comp/env/jdbc/myDb

This is using the latest Glassfish (4.1), the latest Postgres Driver (9.3-1102 JDBC 41) against Postgres 9.4, and the latest Java (1.8.0_31-b13).

1

1 Answers

2
votes

Got to the Menu JDBC -> JDBC Resources -> jdbc/__default

Choose your DBName at the DropDown Menu "Pool Name: " and use

@Resource (mappedName="jdbc/__default")

Mind the two "_" !

At least this works for me.

p.s.: Can you specify where the NPE is thrown ?