- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
运行项目会抛出这个错误:
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/
这两行的状态设置为 1。我想将它们设置为 0 并可视化 rollback 查询的结果。这个查询完成了这项工作: START TRANSACTION; UPDATE jkm_content SET st
通常当我做 $ rake db:rollback 它回滚一次迁移。但现在,反复多次,它已经回滚了 3 个步骤。 这种行为怎么来的? 我在 Rails 3.2.13 上。 最佳答案 您可以指定这样的步骤
不确定我是否在问正确的问题,但是这里... 我已连接到关闭自动提交的 postgres 实例。我看到的是,如果查询存在语法问题: select * fro table limit 400 ,我必须先回
Cassandra 2.0 documentation包含以下关于原子性的段落: For example, if using a write consistency level of QUORUM w
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
我在办公室运行本地主机 XAMPP 服务器用于测试目的。我正在旅行,决定在我的笔记本电脑上完成一些工作,因此我在我的笔记本电脑上安装了新版本的 XAMPP,复制了我的文件,但由于某种原因,我在我的笔记
我在 Grails 3 中使用 Spock 进行测试。一个特定的测试用例被破坏了,因为在这种情况下,Grails 在两个不同的 session 中与我的数据库对话。我的规范用 @Rollback 注释
我是 PostgreSQL/libpq 的新手。所以请帮我澄清我的困惑: 假设我首先执行“START TRANSACTION”并进行适当的错误检查 (PQresultStatus(res) != [p
问题 在遇到数据库完整性错误(SQLAlchemy 的要求)后调用 SQLAlchemy 的 session.rollback() 会导致所有 session 对象被释放。这包括之前选择创建的对象。
与使用 EF Core 管理交易相关的问题: IDbContextTransaction.Rollback() 是否有机会返回异常,以便将其放在 try block 中是否有意义? 即 try
在下面的代码中,当我在表中插入数据时,回滚的目的是什么,如果我想回滚,我不应该插入它,那么使用回滚的合适方法是什么? BEGIN TRANSACTION Insert into dimCustomr
我在 START TRANSACTION 和 COMMIT 之间执行了多个查询 SQL 语句。 如果出现任何问题,进程将在中间终止而不执行 COMMIT 并且数据库不会发生任何事情。到目前为止,没有执
我们可以在 Oracle 中创建保存点,然后我们可以通过调用 ROLLBACK TO SAVEPOINT 回滚到特定的保存点。 . UPDATE employees SET salary =
ROLLBACK TRAN 会回滚我的 SP 中的所有内容吗? (包括对另一个SP的调用) 示例: BEGIN TRAN INSERT INTO (table1) VALUES (1,'abc
我一直在尝试使用我们现有的 Spring JUnit 测试(通过 @TransactionalTestExecutionListener 运行或子类化 AbstractTransactionalUni
在我的托管 Bootstrap 的 bundle.wxs 中,我链接了多个包: 我正在订阅 ExecutePackageBegin/ExecutePackageComplete
此代码使用存储在 $in 中的所有查询进行交易。例如$in = "查询1;查询2;查询3;" 当其中一个失败时,它被回滚但没有效果,没有错误的查询被插入到数据库中 $conn = mysql_
我有以下代码在事务中执行数据库操作: try { //start transaction repository.startTrans
有一天,我注意到我的事务不接受 ActiveRecord::Rollback。我有一个这样的例子: 示例 ActiveRecord::Base.transaction do puts @shi
连接没问题,但是查询语句好像有问题。 query1 = """SELECT * FROM `DATABASE` WHERE `coin` = 'LTC'""" query2 = """SELECT *
我是一名优秀的程序员,十分优秀!