【发布时间】:2015-05-19 23:35:29
【问题描述】:
我知道打开一个事务,等待用户输入和进行事务是一种非常糟糕的编程方式。但是,这是我正在做的一项课程作业所必需的 - 我们不允许更改已提供给我们的表格。
我正在使用具有 runQuery 和 update 方法的数据库访问类。为了使用事务,我创建了一个名为 commit transaction 的新方法 -
public static void commitTransaction(String SQLStatement) throws SQLException,
Exception {
Connection dbConnect = DatabaseAccess.getConnection();
PreparedStatement p = null;
try {
dbConnect.setAutoCommit(false);
p = dbConnect.prepareStatement(SQLStatement);
} catch (Exception e) {
System.out.println("An error has occured");
}
p.executeUpdate();
//dbConnect.setAutoCommit(true);
dbConnect.close();
}
由于 dbConnect.setAutoCommit(false),我创建了一个单独的函数,我认为需要在事务期间允许用户输入。我尝试创建的方法进行安全预订 - 启动事务,等待一堆输入,然后提交事务。我没有收到来自 java 的错误 - 但是没有任何内容添加到数据库中。
我认为我的事务可能未提交的一个原因是我在用户输入中运行查询。我需要输出这些查询的结果集 - 所以我没有使用禁用 autoCommit 的 commitTransaction 方法。这有什么问题吗?如果是这样,我该如何解决?
//Start the transaction
DatabaseAccess.commitTransaction("START TRANSACTION;");
bookingID = BookingErrorChecks.createBookingID();
/////////////////////////////////////////////
//User inputs for flight they wish to book
/////////////////////////////////////////////
System.out.println("Showing available seats on your flight:");
SQLStatement = "SELECT Flight.flightID, flightDate,\n"
+ "(MaxCapacity - SUM(NumSeats))\n"
+ "AS AvailableSeats FROM Flight JOIN\n"
+ " FlightBooking ON Flight.flightID =\n"
+ " FlightBooking.flightID\n"
+ "WHERE (Status = 'R' OR Status = 'H') AND Flight.flightID="
+ flightID
+ "GROUP BY Flight.flightID";
answer = DatabaseAccess.runQuery(SQLStatement);
/////////////////////////////////////////////
//Prints out table of results
/////////////////////////////////////////////
if (Integer.parseInt(availableSeats) < 1)
{
System.out.println("There are no available seats, choose another"
+ "flight");
DatabaseAccess.commitTransaction("ROLLBACK");
} else {
save = DatabaseAccess.setSavepoint();
}
/////////////////////////////////////////////
//User input for the number of seats and their first and last name
/////////////////////////////////////////////
SQLStatement = "SELECT customerID FROM LeadCustomer WHERE FirstName = '"
+ firstName + "' AND Surname = '" + lastName + "'";
answer = DatabaseAccess.runQuery(SQLStatement);
if (answer.next()) {
customerID = answer.getInt("CustomerID");
} else {
/////////////////////////////////////////////
//Create customer
/////////////////////////////////////////////
}
/////////////////////////////////////////////
//Final user inputs about the flight
/////////////////////////////////////////////
SQLStatement = "INSERT INTO FlightBooking values("
+ bookingID + "," + customerID + "," + flightID + ","
+ numSeats + ",'" + status + "','" + time + "',"
+ totalCost+")";
DatabaseAccess.commitTransaction(SQLStatement);
DatabaseAccess.commitTransaction("COMMIT TRANSACTION;");
System.out.println("Flight Booking was sucessfully added.");
}
}
【问题讨论】:
标签: java postgresql jdbc transactions