Currently, I am working in Spring boot with postgres jdbc driver and I have a connection with my database. However, the first time I try to insert, I receive no errors, but on DB side, I see no insertion. On the second try, I get the following error:
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.
On both tries, I am not able to insert. My debugger tells me that I have sent the following:
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)
The database schema I have set up is this:
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);
My java server-side code looks like this:
@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";
}
}
Where have I gone wrong?