Java Reference
In-Depth Information
pstmt.setString(4, text);
pstmt.executeUpdate();
System.out.println("Record successfully
inserted.");
success = true;
} catch (SQLException ex){
success = false;
ex.printStackTrace();
}
return success;
}
}
In the end, if any of the statements fails, all transactions will be rolled back.
However, if all the statements execute properly, everything will be committed.
How It Works
Transaction management can play an important role in an application. This holds true
especially for applications that perform different tasks that depend on each other. In
many cases, if one of the tasks performed within a transaction fails, it is preferable for
the entire transaction to fail rather than having it only partially complete. For instance,
imagine that you were adding database user records to your application database. Now
let's say that adding a user for your application required a couple of different database
tables to be modified, maybe a table for roles, and so on. What would happen if your
first table was modified correctly and the second table modification failed? You would
be left with a partially complete application user addition, and your user would most
likely not be able to access the application as expected. In such a situation, it would be
nicer to roll back all the already-completed database modifications if one of the updates
failed so that the database was left in a clean state and the transaction could be attemp-
ted once again.
By default, a Connection object is set up so that auto-commit is turned on. That
means that each database INSERT , UPDATE , or DELETE statement is committed right
away. Usually, this is the way that you will want your applications to function.
However, in circumstances where you have many database statements that rely on one
another, it is important to turn off auto-commit so that all the statements can be com-
mitted at once. To do so, call the Connection object's setAutoCommit() meth-
Search WWH ::




Custom Search