運行該項目引發此錯誤:調用connection.rollback()拋出NullPointerException異常
java.lang.NullPointerException
com.myproject.crud.EmployeesJDBCImpl.withDB(EmployeesJDBCImpl.java:157)
com.myproject.crud.EmployeesJDBCImpl.doList(EmployeesJDBCImpl.java:119)
com.myproject.crud.EmployeesJDBCImpl.listEmployees(EmployeesJDBCImpl.java:111)
com.myproject.crud.EmployeesJDBCImpl$Proxy$_$$_WeldClientProxy.listEmployees(EmployeesJDBCImpl$Proxy$_$$_WeldClientProxy.java)
com.myproject.crud.EmployeesListServlet.doGet(EmployeesListServlet.java:36)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
的try
塊失敗,就跳轉到catch
塊地方好像connection
對象爲空時connection.rollback()
被調用。很難理解爲什麼會發生這種情況。在context.xml
中提供的用戶名和密碼是正確的。
EmployeesJDBCImpl.java
package com.myproject.crud;
import java.util.ArrayList;
import java.util.List;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.enterprise.context.ApplicationScoped;
import javax.annotation.Resource;
import javax.sql.DataSource;
@ApplicationScoped @JDBC
public class EmployeesJDBCImpl implements EmployeesRepository {
@Resource(name="jdbc/EmployeesDB")
private DataSource dS;
@Override
public Employee viewEmployee(final String id) {
return withDB(new JDBCRunner<Employee>(){
@Override
public Employee run(Connection connection) throws Exception{
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM employees WHERE id = ?");
preparedStatement.setString(1, id);
ResultSet rs = preparedStatement.executeQuery();
if(rs.next()){
Employee employee = new Employee();
employee.setId(rs.getString("id"));
employee.setName(rs.getString("name"));
employee.setPhone(rs.getString("phone"));
employee.setEmail(rs.getString("email"));
return employee;
}else{
return null;
}
}});
}
private <T> T withDB(JDBCRunner<T> runner){
Connection connection = null;
try{
connection = dS.getConnection();
boolean auto = connection.getAutoCommit();
connection.setAutoCommit(false);
T result = runner.run(connection);
connection.commit();
connection.setAutoCommit(auto);
return result;
}catch(Exception e){
try{
connection.rollback(); // Nullpointer exception here
}catch(SQLException ex){
}
//throw new EmployeesException(e);
}finally{
if(connection != null){
try{
connection.close();
}catch(Exception ex){
}
}
}
return null;
}
}
META-INF/context.xml的
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/EmployeesDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="secret" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/javatest"/>
</Context>
WEB-INF/web.xml中
...
<description>MySQL Employees App</description>
<resource-ref>
<description>db connection</description>
<res-ref-name>jdbc/EmployeesDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
...
我同意,但爲什麼不首先正確創建是什麼混淆了我。 – user1701467
@ user1701467看到我更新的答案,你應該記錄錯誤,這將指出你的問題。 – dan
connection = dS.getConnection();你在哪裏初始化數據源? – agirish