数据源在启动连接池时出现NullPointerException
NullPointerException of datasource in initiating the connection pool
所以我在 context.xml 中有一个代码,其中包含连接我的数据库的信息
<?xml version="1.0" encoding="UTF-8"?>
<Resources name="jdbc/murach" auth="Containter"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/murach"
username="root" password="root"
maxActive="100" maxIdle="30" maxWait="10000"
logAbandoned="true" removeAbandoned="true"
removeAbandonedTimeout="60" type="javax.sql.DataSource" />
<ResourceLink name="jdbc/murach"
global="jdbc/mydb"
type="javax.sql.DataSource" />
虽然对于 连接池 我创建了一个 class 来启动并将其连接到数据库。错误始于 getConnection()
的方法。请问我应该在查找中真正放入什么才能使其工作。顺便说一下,datasource.getConnection()
的 return 中的错误是 NullPointerException
package lars.data;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class ConnectionPool {
private static ConnectionPool pool = null;
private static DataSource dataSource = null;
private ConnectionPool() {
try {
InitialContext ic = new InitialContext();
dataSource = (DataSource) ic.lookup("java:/comp/env/jdbc/murach");
System.out.print(dataSource);
} catch (NamingException e) {
System.out.println(e);
}
}
public static synchronized ConnectionPool getInstance() {
if (pool == null) {
pool = new ConnectionPool();
}
return pool;
}
public Connection getConnection() {
try {
return dataSource.getConnection();
} catch (SQLException e) {
System.out.println(e);
return null;
}
}
public void freeConnection(Connection c) {
try {
c.close();
} catch (SQLException e) {
System.out.println(e);
}
}
}
我认为您的问题出在 ResourceLink
的配置中,尤其是应该与 Resource
中定义的值相匹配的属性 global
的值,应该是:
<ResourceLink name="jdbc/murach"
global="jdbc/murach"
type="javax.sql.DataSource" />
global: The name of the linked global resource in the global JNDI
context.
name: The name of the resource link to be created, relative to the
java:comp/env
context.
注意 1: 定义资源的节点名称是 Resource
而不是 Resources
。换句话说应该是:
<Resource name="jdbc/murach" ... />
注意 2: 你应该试试 ic.lookup("java:comp/env/jdbc/murach")
所以我在 context.xml 中有一个代码,其中包含连接我的数据库的信息
<?xml version="1.0" encoding="UTF-8"?>
<Resources name="jdbc/murach" auth="Containter"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/murach"
username="root" password="root"
maxActive="100" maxIdle="30" maxWait="10000"
logAbandoned="true" removeAbandoned="true"
removeAbandonedTimeout="60" type="javax.sql.DataSource" />
<ResourceLink name="jdbc/murach"
global="jdbc/mydb"
type="javax.sql.DataSource" />
虽然对于 连接池 我创建了一个 class 来启动并将其连接到数据库。错误始于 getConnection()
的方法。请问我应该在查找中真正放入什么才能使其工作。顺便说一下,datasource.getConnection()
NullPointerException
package lars.data;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class ConnectionPool {
private static ConnectionPool pool = null;
private static DataSource dataSource = null;
private ConnectionPool() {
try {
InitialContext ic = new InitialContext();
dataSource = (DataSource) ic.lookup("java:/comp/env/jdbc/murach");
System.out.print(dataSource);
} catch (NamingException e) {
System.out.println(e);
}
}
public static synchronized ConnectionPool getInstance() {
if (pool == null) {
pool = new ConnectionPool();
}
return pool;
}
public Connection getConnection() {
try {
return dataSource.getConnection();
} catch (SQLException e) {
System.out.println(e);
return null;
}
}
public void freeConnection(Connection c) {
try {
c.close();
} catch (SQLException e) {
System.out.println(e);
}
}
}
我认为您的问题出在 ResourceLink
的配置中,尤其是应该与 Resource
中定义的值相匹配的属性 global
的值,应该是:
<ResourceLink name="jdbc/murach"
global="jdbc/murach"
type="javax.sql.DataSource" />
global: The name of the linked global resource in the global JNDI context.
name: The name of the resource link to be created, relative to the
java:comp/env
context.
注意 1: 定义资源的节点名称是 Resource
而不是 Resources
。换句话说应该是:
<Resource name="jdbc/murach" ... />
注意 2: 你应该试试 ic.lookup("java:comp/env/jdbc/murach")