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

标签 java mysql 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 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/

相关文章:

java - 我应该重写 Object.equals(Object) 方法吗?

java processbuilder 打开一个文本文件读取数据

java - p :fileUpload prime faces

java - 报告优化问题

java - 线程问题 : NullPointerException

java - 如何为 jdbc 模拟上下文数据源

php - Jquery自动补全+Mysql

sql - MySQL 不同条目忽略 ORDER_BY 之后的所有重复

JAVA: Swing JButton 组件 (NullPointerException)

java - 对包含空元素的数组进行排序