简体   繁体   English

尝试将JNDI用于JDBC时出现空指针异常

[英]Null pointer exception when attempting to use JNDI for JDBC

In Glassfish admin, I setup a JDBC connection pool that I can ping ok. 在Glassfish管理员中,我设置了一个我可以ping通的JDBC连接池。 Also in Glassfish admin, I have setup a JNDI JDBC resource called jdbc/myDb 同样在Glassfish管理员中,我已经设置了一个名为jdbc / myDb的JNDI JDBC资源。

However I'm unable to connect to it from Java. 但是我无法从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. 我得到的只是java.lang.NullPointerException。

I have also tried various naming styles such as java:jdbc/myDB or java:comp/env/jdbc/myDb 我也尝试了各种命名样式,例如java:jdbc / myDB或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). 它使用最新的Glassfish(4.1),针对Postgres 9.4的最新Postgres驱动程序(9.3-1102 JDBC 41)和最新的Java(1.8.0_31-b13)。

Got to the Menu JDBC -> JDBC Resources -> jdbc/__default 转到菜单JDBC-> JDBC资源-> jdbc / __ default

Choose your DBName at the DropDown Menu "Pool Name: " and use 在下拉菜单“池名称:”中选择您的DBName并使用

@Resource (mappedName="jdbc/__default")

Mind the two "_" ! 注意两个“ _”!

At least this works for me. 至少这对我有用。

ps: Can you specify where the NPE is thrown ? ps:您可以指定将NPE扔到哪里吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM