Using a database transaction with JDBC



This example demonstrates how to use a transaction with JDBC.
The only thing really that differs from ordinary database operations is that you set a boolean value on the Connection object.
The Connection object has a method called setAutoCommit which sets a value of the object that decides whether to execute the query immediately or not.
Default that value is true, so in order to utilize a transaction you need to set it to false. In the example a new connection is created and finally also closed.
It is though more common to have a pool of connections from which you borrow a connection when you need it and then return it to the pool once you are finished with it.
If that were the case here we would have to call the setAutoCommit method again in the finally block, this time with false as the argument, before returning it to the pool.







Search for more Java info on this site here:
Custom Search


Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">