我从类路径中列为导出依赖项的.jar文件中看到一个看似无法解释的nullPointerException。
jar文件包含一个数据库bean,它在普通主方法POJO中调用时可以正常工作,但是当我在servlet中调用相同的jar文件时,我从 con.prepareStatement获取空指针无论我打电话。
我认为它可能是来自池的SQLException导致它,所以我把它移动到我们的VM服务器,它可以在它的云环境中直接与数据库实例对话,我也得到同样的错误。
bean包含四个额外的依赖jar文件: guava-16.0.1.jar (对于弱引用的哈希映射), postgresql-9.3-1100.jdbc41.jar , tomcat-dbcp.jar , tomcat-jdbc.jar 这些都是使用FatJar for Eclipse包装到一个jar文件中,然后导入到我的servlet中,这是托管在Tomcat8 servlet容器中(也在Tomcat7和TomEE中测试过)。
servlet代码如下:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.package.DBEngine;
@WebServlet("/PrimaryEngine")
public class PrimaryEngine extends HttpServlet {
private static final long serialVersionUID = 1L;
private DBEngine db;
public void init(ServletConfig config) throws ServletException{
super.init(config);
db = new DBEngine();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
if(request.getParameterMap().containsKey("op")){
out.println("Getting data...");
String test = db.DBdebug();
out.println(test);
return;
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
DBEngine看起来像这样:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.ConcurrentMap;
import java.util.logging.Level;
import com.google.common.collect.MapMaker;
import com.sun.istack.internal.logging.Logger;
public class DBEngine {
private Pool pool = null;
private Connection con = null;
private PreparedStatement ps = null;
//-------------------------------------------
public DBEngine(){
this.pool = new Pool();
this.con = pool.getConnection();
this.ps = null;
}
//---------------------------------------------------
// Begin subsidiary functions/methods
//---------------------------------------------------
public String DBdebug(){
try {
String ps = "SELECT table_name FROM information_schema.tables";
PreparedStatement ps2 = con.prepareStatement(ps);
ResultSet rs = ps2.executeQuery();
if(rs.next()){
return "Success";
} else return "Fail";
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
tomcat-dbcp池如下所示:
import java.sql.Connection;
import java.sql.SQLException;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolProperties;
public class Pool {
private static DataSource datasource = null;
//--------------------------------------------------
// Sets up the database connection pool
//--------------------------------------------------
public Pool() {
PoolProperties p = new PoolProperties();
p.setUrl("jdbc:postgresql://*URL*:5432/*databasename*");
p.setDriverClassName("org.postgresql.Driver");
p.setUsername("*someUserName*");
p.setPassword("*somePassword*");
p.setJmxEnabled(true);
p.setTestWhileIdle(false);
p.setTestOnBorrow(true);
p.setValidationQuery("SELECT 1");
p.setTestOnReturn(false);
p.setValidationInterval(30000);
p.setTimeBetweenEvictionRunsMillis(30000);
p.setMaxActive(75);
p.setMaxIdle(75);
p.setInitialSize(10);
p.setMaxWait(10000);
p.setRemoveAbandonedTimeout(60);
p.setMinEvictableIdleTimeMillis(30000);
p.setMinIdle(10);
p.setLogAbandoned(true);
p.setRemoveAbandoned(true);
p.setRemoveAbandonedTimeout(60);
p.setAbandonWhenPercentageFull(60);
p.setJdbcInterceptors(
"org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"+
"org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer;"+
"org.apache.tomcat.jdbc.pool.interceptor.ResetAbandonedTimer");
datasource = new DataSource();
datasource.setPoolProperties(p);
}
//-------------------------------------------------------
// Closes an open datasource
//-------------------------------------------------------
public synchronized void closeDatasource() {
try {
datasource.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//--------------------------------------------------------
// Get a connection
//--------------------------------------------------------
public Connection getConnection() {
try {
return datasource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
堆栈跟踪:
[ajp-nio-8009-exec-3] org.apache.catalina.core.StandardWrapperValve.invoke Allocate exception for servlet com.package.engine.main.PrimaryEngine
java.lang.NullPointerException
com.package.DBEngine.<init>(DBEngine.java:##)
com.package.main.PrimaryEngine.init(PrimaryEngine.java:26)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:74)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:509)
org.apache.coyote.ajp.AbstractAjpProcessor.process(AbstractAjpProcessor.java:799)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:651)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1575)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1533)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
java.lang.Thread.run(Thread.java:722)
我在这里结束了。我所知道的关于Java的一切都说它应该有用,但事实并非如此。
编辑:
这是Tomcat输出的唯一其他堆栈跟踪:
javax.servlet.ServletException:servlet com.package.main.PrimaryEngine的Servlet.init()抛出异常 org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:74) org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:509) org.apache.coyote.ajp.AbstractAjpProcessor.process(AbstractAjpProcessor.java:799) org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:651) org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1575) org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1533) java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:615) java.lang.Thread.run(Thread.java:722)
答案 0 :(得分:1)
基于Luiggi关于不在类中手动创建数据源的建议并建议使用JNDI数据库池,我能够得到下面的解决方案。 我的改编很大程度上基于以下链接:http://www.codejava.net/servers/tomcat/configuring-jndi-datasource-for-database-connection-pooling-in-tomcat
Pieces:Tomcat 8.0,postgresql-jdbc驱动程序,DBEngine bean,servlet
dbEngine bean由三个类组成:DBEngine.java,Pool.java和SQLPS.java(SQL语句库),它们都包装在一个jar文件中并导入到servlet中并标记为类路径导出依赖项。
调用bean的servlet看起来像:
package com.engine.main;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.concurrent.ConcurrentMap;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.AsyncContext;
import javax.servlet.AsyncEvent;
import javax.servlet.AsyncListener;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import sslog4java.LogLevel;
import sslog4java.Logger;
import com.google.gson.Gson;
import com.DBbean.DBEngine;
@WebServlet("/PrimaryEngine")
public class PrimaryEngine extends HttpServlet {
private static final long serialVersionUID = 1L;
private DBEngine db = null;
private static String filePath = "";
private static String fileName = "";
Logger fLogger = null;
public void init(ServletConfig config) throws ServletException{
super.init(config);
ServletContext scxt = config.getServletContext();
filePath = scxt.getRealPath("/WEB-INF") + "\\logs\\";
fileName = "loggerFileName";
fLogger = new Logger(filePath, fileName, LogLevel.DEBUG);
try {
// passed the servlet context into the DBengine for the pool to use
db = new DBEngine(new InitialContext(), fLogger);
} catch (SQLException | NamingException e) {
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
if(request.getParameterMap().containsKey("param") && request.getParameter("param").equals("paramValue")){
test = db.DBdebug();
out.println(test);
return;
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
dbEngine.java看起来像:
package com.DBbean;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.ConcurrentMap;
import javax.naming.Context;
import sslog4java.Logger;
import com.google.common.collect.MapMaker;
public class DBEngine {
private Pool pool = null;
private Connection con = null;
private Logger fLogger;
public DBEngine(Context initCtx, Logger logr) throws SQLException{
this.fLogger = logr;
// passed the servlet context into the Pool.java
this.pool = new Pool(initCtx);
this.con = pool.getConnection();
}
public String DBdebug(){
Connection conn = pool.getConnection();
try {
String ps = SQLPS.debugSQL;
PreparedStatement ps2 = conn.prepareStatement(ps);
ResultSet rs = ps2.executeQuery();
if(rs.next()){
return "Success";
} else return "Fail";
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
并且Pool.java看起来像:
package com.DBbean;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class Pool {
Context ctx;
public Pool(Context context){
ctx = context;
}
public Connection getConnection() {
try {
DataSource ds = (DataSource) ctx.lookup("java:/comp/env/jdbc/postgres4" );
return ds.getConnection();
} catch (SQLException | NamingException sne) {
sne.printStackTrace();
return null;
}
}
}
我必须将这些行添加到$ CATALINA_HOME / conf / context.xml:
<Resource
name="jdbc/postgres4"
auth="Container"
type="javax.sql.DataSource"
maxActive="8"
maxIdle="8"
driverClassName="org.postgresql.Driver"
url="*URL*"
username="*UserName*"
password="*Password*"
/>
这些行到$ CATALINA_HOME / conf / web.xml:
<resource-ref>
<description>postgreSQL Datasource</description>
<res-ref-name>jdbc/postgres4</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
并且postgresql-jdbc jar进入$ CATALINA_HOME / lib