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

  • Home
  • SEARCH
  • 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 1808604
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:16:56+00:00 2026-05-17T06:16:56+00:00

hello i’m a beginner java developer and this is one question in a list

  • 0

hello
i’m a beginner java developer and this is one question in a list i’m posting since i’ve started porting a very old web service . I’m trying to improve the access to db and i’m finding a lot of code i believe migth be dangerous, but being not so experienced with java i cannot be sure .
Actually i’ve a class which manages the db connection and holds static references to a connection and a statement objects : it exposes an “openDb” method which initializes the sql connection class member variable.

Problem number 1 : the class member variables are static, what is going to happen if the openDb is called multiple time and the first (for example) instance of the class is still executing a query?

the above class expose a method name “closeDb” which releases all resources (connection and statement both static members! ) and a “closeStatement” method which release only the statement member, and is used externally.

Problem number 2 : stements and resultset must be closed after a transaction committ/rollback or can be closed immediatly?

Still the same class handles the commit/rollback exposing some methods (to set autocommit to false, to commit or rollback ) . An instance of this class is passed to other classes instance (for example the usual employee department classes ) which execute their own queries on the db and finally the commit/rollback is called by the connection manager class . Is this “architecture” correct or do you see any danger in it ?

thank you in advance

  • 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-17T06:16:56+00:00Added an answer on May 17, 2026 at 6:16 am

    This is a very bad design and the observations you make concerning the static variables are correct. If one thread calls openDb and another thread does the same, things will go wrong. Maybe this happens not all the time which makes this hard to debug.

    Concerning problem nr. 2: you should close things once you’re done with them. This way you don’t keep resources occupied unnecesary. So when you have read all (necessary) data from a result set, close it. It’s also not a good idea to have statements and result sets escape the transaction boundary. So instead of:

    begin transaction
      open result set
    commit transaction
    read from result set
    

    you should have:

    begin transaction
      open result set
      read from result set
    commit transaction
    

    To make the picture complete, you should always have the following pattern when using transactions:

    begin transaction
      open connection
        read/modify data
      close connection
    commit transaction
    

    The open connection/close connection combination may occur multiple times within one transaction.

    EDIT: To complete the picture even more: in the usual scenario, your application has several layers (e.g. UI, business, data). Transactions are usually started and committed inside the business layer. From the business layer you call one or more data layer methods that become a part of the transaction:

    // Inside business layer:
    public void businessMethod(...) {
        try {
            // Begin transaction.
            dataMethod1(...);
            dataMethod2(...);
            ...
            // Commit transaction.
        }
        catch (...) {
        }
    }
    
    // Inside data layer.
    public void dataMethod1(...) {
        // Open connection.
        try {
            // Get data from database.
            // Manipulate data.
        }
        finally {
            // Close connection.
        }
    }
    

    Some additional notes:

    • The transaction commit is the final statement inside the try/catch (if you want to return data from your method, it can be followed by a return statement). This ensures that the transaction only commits when everything is ok (i.e. no exception thrown).
    • The connection close is inside the finally part of the try statement. This ensures that it is always closed, no matter what happens inside your data method.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello again ladies and gents! OK, following on from my other question on ASP.NET
Hello can anybody solve this please I'm creating the object in the action class
Hello all you helpful folks @ stackoverflow! Best resources for Java GUI's? Looking at
Hello I am working with a simulator that uses rcS scripts to boot, this
Hello everyone i need to convert this code to a function with 2 parameters
Hello in my java class Toto, I have 3 static methods I would like
Hello I have the following error by git-fsck, which cannot be cleaned by git-gc
Hello I was writing a Regular Expression (first time in my life I might
Hello I am compiling a program with make but I get the error of
Hello , I am currently working on the student association's website. The student's association

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.