【发布时间】:2016-07-16 16:57:16
【问题描述】:
目前,我正在使用 postgres jdbc 驱动程序在 Spring Boot 中工作,并且与我的数据库建立了连接。但是,我第一次尝试插入时,没有收到任何错误,但在 DB 端,我看不到插入。在第二次尝试时,我收到以下错误:
Cannot get out of auto-commit mode with error: org.postgresql.util.PSQLException: This connection has been closed.
SQLException: org.postgresql.util.PSQLException: This connection has been closed.
在两次尝试中,我都无法插入。我的调试器告诉我,我发送了以下内容:
preparedStatement: INSERT INTO event (StartTime, EndTime, Description, name, DisplayPicLoc, attendenceCount) VALUES ('2016-2-29 19:0:0.000000 -5:0:0','2016-3-1 19:0:0.000000 -5:0:0','b','b',NULL,0)
我设置的数据库架构是这样的:
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE TABLE event (eventID UUID DEFAULT uuid_generate_v4() NOT NULL primary key,
StartTime timestamp NOT NULL,
EndTime timestamp NOT NULL,
Description varchar(1500),
name varchar(80) NOT NULL,
DisplayPicLoc varchar(80),
attendenceCount int);
我的 java 服务器端代码如下所示:
@Controller
public class CalendarController {
@RequestMapping(value="/event", method=RequestMethod.POST)
public @ResponseBody
String uploadEvent(@RequestParam("title") String title,
@RequestParam("description") String description,
@RequestParam("start") Date start,
@RequestParam("end") Date end){
Timestamp startTime = null;
Timestamp endTime = null;
try{
startTime = new Timestamp(start.getTime());
endTime = new Timestamp(end.getTime());
}
catch(Exception e){
System.err.print("Date conversion error: " + e);
return "failure";
}
System.out.println("Event received with Title: " + title +
" Description: " + description +
" Start: " + startTime +
" End: " + endTime);
Savepoint savepoint = null;
Connection connection = DatabaseConnection.connection;
try{
connection.setAutoCommit(false);
}
catch(SQLException e){
System.err.println("Cannot get out of auto-commit mode with error: " + e);
}
if(connection==null){
new DatabaseConnection();
}
try{
savepoint = connection.setSavepoint();
String query = "INSERT INTO event " +
"(StartTime, EndTime, Description, name, DisplayPicLoc, attendenceCount) " +
"VALUES (?,?,?,?,?,?);";
PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement.setTimestamp(1, startTime);
preparedStatement.setTimestamp(2, endTime);
preparedStatement.setString(3, description);
preparedStatement.setString(4, title);
preparedStatement.setString(5, null);
preparedStatement.setInt(6, 0);
System.out.println("preparedStatement: " + preparedStatement);
preparedStatement.executeUpdate();
//connection.close();
return "success";
}
catch(SQLException e){
System.err.println("SQLException: " + e);
if(connection!=null && savepoint!=null){
try{
connection.rollback(savepoint);
}
catch(SQLException error){
System.err.println("SQLException: " + error);
}
}
}
finally{
if(connection != null) {
try {
connection.close();
}
catch(SQLException e) {
System.err.println("SQLException: " + e);
}
}
}
return "failure";
}
}
我哪里出错了?
【问题讨论】:
-
您的代码中没有
commit() -
你能告诉我们
DatabaseConnection类的代码,特别是DatabaseConnection.connection;是如何声明和初始化的吗?它看起来像是在不同线程之间共享的静态变量。为什么不使用连接池? -
@a_horse_with_no_name 我应该把 commit() 放在哪里?我在网上查了一下,但我不确定我是否理解。
-
@kordirko 我的连接在不同的类之间静态共享。我不知道什么是连接池。
标签: spring postgresql jdbc spring-boot