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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:51:32+00:00 2026-06-04T13:51:32+00:00

I am having the following approach for logging certain things from my java application

  • 0

I am having the following approach for logging certain things from my java application to Oracle DB.

Package com.util.dblog;
public class DBLog {

static Connection con = null;
static PreparedStatement stmt = null;

static { 
 try{
      DBConnectionHelper connHelper = DBConnectionHelper.createInstance();
    con=connHelper.getConnection("ds"); //Getting connection from connection pool
    con.setAutoCommit(false);
    }
catch(Exception e)
{}  
       } 

public static void logmethod1(String param1, String param2) { 

if (con == null || con.isClosed()) {
 DBConnectionHelper connHelper = DBConnectionHelper.createInstance();
    con=connHelper.getConnection("ds");
    con.setAutoCommit(false);
}

String SQL_PREP_INSERT = "INSERT INTO tableA (LOG_ID,USER_ID,EXEC_TIME) VALUES"             + " (logid_seq.nextval, ?, ?)"; 
stmt = con.prepareStatement(SQL_PREP_INSERT); 
stmt.execute();
stmt.close();
}

public static void logmethod2(String param1, String param2, String param3) { 

if (con == null || con.isClosed()) {
 DBConnectionHelper connHelper = DBConnectionHelper.createInstance();
    con=connHelper.getConnection("ds");
    con.setAutoCommit(false);
}

...
...
}

public saveCon() {
con.commit();
}
public closeCon() {
   con.close();
}

} //End of DBLog class

From my Java application class, I am calling as follows,

import com.util.dblog;
public class myApp{
DBLog.logmethod1(param1, param2);
....
DBLog.logmethod2(param1, param2, param3);
...
} //End of myApp class

And I am calling saveCon() at request level inside Filter class and closeCon() inside destroy() method of sessionListener class. Instead of comitting the connection object inside logmethod1 and logmethod2, I am doing this just to reduce the commit frequency. This methods will be called some 5-10 times in my application. So I will be commiting once in 10 times instead of 10 times per request. Suppose there is another http request there wil be another commit once the 10 logs are inserted. And finally I am closing the connection once session is about to be destroyed.

Now the question I am having is, the connection object “con” that is created in static block of DBLog class will be available for the static methods logmethod1 and logmethod2 ?
I hope its created when the class is loaded first and will be available throughout the application scope?
Because there will be many users coming in and I am worried if this approach is correct, or any other approach is needed?
Please let me know if its not clear and I will get back.

  • 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-06-04T13:51:33+00:00Added an answer on June 4, 2026 at 1:51 pm

    static {} block will be called before your methods, but this is a bad idea to use your class this way. Avoid static constructions when you can substitute them with classic constructions. You can change all variables and methods to non-static, block static {} change to constructor and your App will be something like:

    import com.util.dblog;
    public class myApp{
    
        DBLog log = new DBLog();
    
        log.logmethod1(param1, param2);
        log.logmethod2(param1, param2, param3);
    
    } //End of myApp class
    

    There is a great book called Java Puzzlers at this site, I advise you to read it.
    It has a small chapter related to your question, it will take about 3 minutes to read.

    Java Puzzlers Go to Puzzle 5: Larger Than Life

    Your program will work normally I think.

    There is an alternative without static blocks:

    Package com.util.dblog;
    public class DBLog {
    
    Connection con = null;
    PreparedStatement stmt = null;
    
    public DBLog () {
        init();
    }
    
    private void init() {
        try {
            DBConnectionHelper connHelper = DBConnectionHelper.createInstance();
            con=connHelper.getConnection("ds"); //Getting connection from connection pool
            con.setAutoCommit(false);
        } catch(Exception e) {}  
    }
    
    public void logmethod1(String param1, String param2) { 
    
        if (con == null || con.isClosed()) {
            init();
        }
    
        String SQL_PREP_INSERT = "INSERT INTO tableA (LOG_ID,USER_ID,EXEC_TIME) VALUES"             + " (logid_seq.nextval, ?, ?)"; 
        stmt = con.prepareStatement(SQL_PREP_INSERT); 
        stmt.execute();
        stmt.close();
    }
    
    public void logmethod2(String param1, String param2, String param3) { 
    
        if (con == null || con.isClosed()) {
            init();
        }
        ...
        ...
    }
    
    public void voidsaveCon() {
        con.commit();
    }
    public void closeCon() {
        con.close();
    }
    
    } //End of DBLog class
    
    From my Java application class, I am calling as follows,
    
    import com.util.dblog;
    public class myApp {
    
        ...
        // in some method
        DBLog log = new DBLog();
        log.logmethod1(param1, param2);
        ....
        log.logmethod2(param1, param2, param3);
        ...
    
    } //End of myApp class
    

    If you want to send your variable you must not create another instance. You can send variable in 2 ways:

    SomeAnotherClass {
    
        // after initialize you can use this log, this log is same as in myApp class
        DBLog log;
    
        // 1 way: by sending to constructor
        public SomeAnotherClass(DBLog log) {
            this.log = log;
        }
    
        // 2 way: by calling set method
        public void setLog(DBLog log) {
            this.log = log;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble figuring out the best approach for an certain application. I'm not
I'm having following problem. I should convert a control to a certain type, this
I am creating package using cmake I am having following structure bin/ bin1 lib/
Having the following class: public class SomeClass { private readonly int[] _someThings; public SomeClass()
I am writing a GAE application and am having some difficulty with the following
Following this blog article I enabled my application to load i18n messages from the
I am developing application having business objects created from EF 4.0. The application is
I am having following peice of code ,where in i am trying to serialize
I'm currently having following error message when executing a .sql file with about 26MB
I am having following issue with reading binary file in C. I have read

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.