Trying to do a rollback, when the rollback kicks in it throws an SQL Exception. What am I doing wrong??? I've checked the other posts but cant find anything to explain how to fix this. I've tried using savepoints and it says those are not able to be implemented. Do rollbacks always throw this error? Using MySQL Server and northwinds DB.
Stack Trace:
TESTING TRANSACTION FAILURE
Connecting to database at jdbc:mysql://localhost:3306/northwind ...
Connecting...
Checking for transaction support...
Beginning a transaction...
Looking up customer 'ACJO' ...
Connecting to database at jdbc:mysql://localhost:3306/northwind ...
Connecting...
select * from customers where CustomerID = ?
Found customer.
Adding a new order to the database...
> insert into orders(OrderDate) values(?)
> select OrderID from orders where OrderDate = ?
Updating an order...
> update orders set orders.BadField = ? where orders.OrderID = ?
**** Rolling back transaction ****
java.sql.SQLException: Unknown column 'orders.BadField' in 'field list'
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1997)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1167)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1278)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2247)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1371)
at advanced.jdbc.northwind.TransactionDemo.updateOrderMistake(TransactionDemo.java:281)
at advanced.jdbc.northwind.TransactionDemo.testTransactionFailure(TransactionDemo.java:216)
at advanced.jdbc.northwind.TransactionDemo.main(TransactionDemo.java:425)
CODE:
public void testTransactionFailure() {
System.out.println("\n\nTESTING TRANSACTION FAILURE");
Connection connection = null;
try {
connection = getConnection();
System.out.println("Checking for transaction support...");
// TODO 04. Get the database metadata.
DatabaseMetaData databaseMetadata = connection.getMetaData();
// TODO 05. Determine if the database supports transactions.
boolean supported = false;
supported = databaseMetadata.supportsTransactions();
if (supported) {
System.out.println("Beginning a transaction...");
// TODO 06. Begin database transaction.
connection.setAutoCommit(false);
// create a new customer
String companyName = "ACME Products Ltd.";
String contactName = "John Doe";
String customerID = createCustomerID(contactName, companyName);
// check for customer before inserting
if (!customerExists(customerID)) {
addCustomer(customerID, companyName, contactName);
}
// add the order
int orderID = insertOrder(connection, customerID);
// oops, forgot shipping city so let's update it
updateOrderMistake(connection, orderID, "Timbuktu");
// TODO 07. Complete the transaction.
System.out.println("Committing");
connection.commit();
} else {
System.err.println("Transactions not supported!");
}
} catch (SQLException e) {
if (connection != null) {
System.out.println("**** Rolling back transaction ****");
// TODO 08. Roll back the transaction.
try {
connection.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
e.printStackTrace();
} finally {
try {
if (connection != null) {
// TODO 09. Release resources.
connection.close();
connection = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}