问题
I know that opening a transaction , waiting for user input and the carrying on with a transaction is a very bad way of programming. However it is required for a piece of coursework I am doing - we are not allowed to alter the table which has been given to us.
I am using a database access class which has a runQuery and update method. To use a transaction I have created a new method called 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();
}
I have created a separate function due to the dbConnect.setAutoCommit(false), which I think is needed to allow user input during the transaction. The method I am trying to create makes a safe booking - starts the transaction, waits for a bunch of inputs and then commits the transaction. I am not getting an error from java - however nothing gets added to the database.
One reason I think my transaction might not be committing is I run queries within the user inputs. I need the result set from these query to be output - so I am not using the commitTransaction method which has the autoCommit disabled. Could this be whats wrong? If so how can I work around it?
//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.");
}
}
回答1:
After dbConnect.setAutoCommit(false);
you need to either call dbConnect.commit()
or change the auto-commit mode like dbConnect.setAutoCommit(true);
.
来源:https://stackoverflow.com/questions/29100808/jdbc-transaction-with-user-input