プロジェクトを実行すると、次のエラーがスローされます。
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
従業員JDBCImpl.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>
...