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 7036125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:23:12+00:00 2026-05-28T01:23:12+00:00

Code I inherited has many transaction code methods of this form: public void addCourseToCourses(String

  • 0

Code I inherited has many transaction code methods of this form:

public void addCourseToCourses(String values)
{
    try
    {
        conn.setAutoCommit(false);
    }
    catch (SQLException e)
    {
        Logger.getLogger(connDb.class.getName()).log(Level.SEVERE, null, e);
        return;
    }
    try
    {
        stmt.executeUpdate("insert into courses values " + values);
        conn.commit();
    }
    catch (SQLException e)
    {
        try
        {
            conn.rollback();
        }
        catch (SQLException ex)
        {
            Logger.getLogger(connDb.class.getName()).log(Level.SEVERE,null, ex);
        }
    }
    finally
    {
        try
        {
            conn.setAutoCommit(true);
        }
        catch (SQLException ex)
        {
            Logger.getLogger(connDb.class.getName()).log(Level.SEVERE,null, ex);
        }
    }
}

Where the variable part that differs between methods is

stmt.executeUpdate("insert into courses values " + values);

Some times it is several inserts, sometimes it is deletes.
I Miss using Macros coming from C++, but I am sure there is a way not to repeat all this transaction code for each method.

help ?

(conn and stmt are class members of types java.sql.Connection and java.sql.Statement)

  • 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-28T01:23:12+00:00Added an answer on May 28, 2026 at 1:23 am

    Create a method that takes an interface and wraps up the exception handling.

    Each interface’s anonymous (or not) implementation contains the SQL call, its parameters, etc.

    For example (very roughly):

    public void addCourseToCourses(final String values) {
        handleSql(new SqlCommand() {
            @Override public void run(Statement stmt) {
                stmt.executeUpdate("insert into courses values " + values);
            }
        });
    }
    

    The handleSql is a static import of something resembling:

    public class SqlWrapper {
        public static void handleSql(SqlCommand cmd) {
            Connection conn = // get connection;
            try {
                conn.setAutoCommit(false);
            } catch (SQLException e) {
                LOG.log(Level.SEVERE, null, e);
                return;
            }
    
            try {
                cmd.run();
                conn.commit();
            } catch (SQLException e) {
                cleanRollback();
            } finally {
                cleanClose();
            }
        }
    }
    

    Various hooks can be added as seem reasonable. A generic version would allow various return types, which is likely more appropriate, just depends on what you actually need.

    A comment mentions Runnable and Callable, IMO Runnable is specifically for threads (and the base interface isn’t generic). Callable is a better choice, but I’d expect enough other hooks would be added to handle SQL-/JDBC-specific functionality that I’d use something app-specific. YMMV.

    This pattern has been re-invented all over the place; it may make more sense to take something like Spring JDBC and just use that.

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

Sidebar

Related Questions

I have some C# / asp.net code I inherited which has a textbox which
I have inherited a load of VB6 code which has tons of individual OCX
I have the following code (inherited from someone): <div class=feedback>&nbsp;</div> <form onsubmit=return false autocomplete=off>
I'm refactoring some code I inherited from a long-gone developer, and I find this:
I've inherited some code and wanted to run this modification by you all, my
In my code I have a Category model which has many subcategories, and the
I've inherited a flash project (my first) that has many existing symbols in the
I have some code I inherited which has a lot of warnings that I
Imagine a method with the following signature: public void ExampleMethod(string id, object data, ref
I've got some inherited code that has a tendency to pass objects around as

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.