gpt4 book ai didi

java - 调用 connection.rollback() 抛出 NullPointerException

转载 作者:太空宇宙 更新时间:2023-11-03 11:04:24 25 4
gpt4 key购买 nike

运行项目会抛出这个错误:

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 block 失败,它跳转到 catch block ,当 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>
...

最佳答案

尝试记录 try block 中的异常。

最有可能的是,这一行会抛出异常: connection = dS.getConnection();

一般来说,您应该尽可能避免捕获所有异常。并记录异常,以便您了解发生了什么。顾名思义,因为异常是不需要的。

--待定

关于java - 调用 connection.rollback() 抛出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13072876/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com