Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7511981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:43:33+00:00 2026-05-29T23:43:33+00:00

How to integrate the common JDBC idiom of creating/receiving a connection, querying the database

  • 0

How to integrate the common JDBC idiom of creating/receiving a connection, querying the database and possibly processing the results with Java 7’s automatic resource management, the try-with-resources statement? (Tutorial)

Before Java 7, the usual pattern was something like this:

Connection con = null;
PreparedStatement prep = null;

try{
    con = getConnection();
    prep = prep.prepareStatement("Update ...");
    ...
    con.commit();
}
catch (SQLException e){
    con.rollback(); 
    throw e;
}
finally{
    if (prep != null)
        prep.close();
    if (con != null)
        con.close();
}

With Java 7 you can go for:

try(Connection con = getConnection(); PreparedStatement prep = con.prepareConnection("Update ..."){

   ...
   con.commit();
}

This will close the Connection and the PreparedStatement, but what about the rollback? I cannot add a catch clause containing the rollback, because the connection is only available within the try block.

Do you still define the connection outside of the try block? What is the best practice here, especially if connection pooling is used?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T23:43:35+00:00Added an answer on May 29, 2026 at 11:43 pm
    try(Connection con = getConnection()) {
       try (PreparedStatement prep = con.prepareConnection("Update ...")) {
           //prep.doSomething();
           //...
           //etc
           con.commit();
       } catch (SQLException e) {
           //any other actions necessary on failure
           con.rollback();
           //consider a re-throw, throwing a wrapping exception, etc
       }
    }
    

    According to the oracle documentation, you can combine a try-with-resources block with a regular try block. IMO, the above example captures the correct logic, which is:

    • Attempt to close the PreparedStatement if nothing goes wrong
    • If something goes wrong in the inner block, (no matter what is is) roll back the current transaction
    • Attempt to close the connection no matter what
    • If something goes wrong closing the connection, you can’t rollback the transaction (as that’s a method on the connection, which is now in indeterminate state), so don’t try

    In java 6 and earlier, I would do this with a triply nested set of try blocks (outer try-finally, middle try-catch, inner try-finally). ARM syntax does make this terser.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple Java application and I'm trying to integrate Hibernate in Spring
We are planning to integrate a hierarchical taxonomy in our software solution. (Java based)
I am working to integrate unit testing into the development process on the team
I'm trying to integrate running Fitnesse tests from MSBuild im my nightly build on
I'd like to integrate a Python IDLE-esque command prompt interface into an existing NI-CVI
I am looking to integrate Crystal Reports 2008 into a Windows Forms application. I
We are trying to integrate tests in our daily builds using TestComplete, so far
I have tried to integrate the Picasa API on iPhone, compiles fine, but I
My goal is to integrate testing into my development environment (as post-build step). I
I need to integrate a email client in my current python web app. Anything

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.