Can I make the method getting the connection from connection pool  Synchronize

Posted on 2006-05-03
Last Modified: 2010-04-17
Hi All,
Can I make the method getting the connection from connection pool  Synchronize?

Plz let me know

Question by:hkishoreb
    LVL 11

    Accepted Solution

    Yes you can make such method synchronized.

    Author Comment

    But my problem here is the server is hanging up  when the load is high on it.  What is the solution to avoid that kind of hang ups

    Thank you
    LVL 11

    Expert Comment

    what do you mean by hanging up?

    Author Comment

    Sorry for the late reply.

    Here is the more description abouit the problem.

    I made the method that is getting the DataSource from the JNDI context as Synchronized.  Max pool capacity as 20.
    This is working fine if the load is less on the server. But at the Peak load all the connections are busy and Server is trying to create more pooled connections. At this point because of the Synchronized method in getting the connections We are seeing slow responce (time delays) in getting the connections from newly created pool . Here is the code that put in the get connection method.

    public synchronized static final Connection getConnection(int iWhichConnectionPool)
                                                              throws Exception
               // create instance of Weblogic's JDBC pool driver
               Log.write("DatabaseManager.getConnection(int): BEGIN");
               java.sql.Connection conn = null;
               Context ctx = null;

               // extra check in case this static class was garbage collected
             if ((m_aNumOpenConnections == null) || (m_aConnectionPoolNames == null))

           Properties env = new Properties();
           String propsFilePath=System.getProperty("propfile");
           String url = System.getProperty("url");
           env.put(Context.PROVIDER_URL, url);
           env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");

                ctx = (Context) new InitialContext(env);
                   javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("jdbc/dataSrc");
                   conn = ds.getConnection();
               catch (Exception e)
                   Log.write("DatabaseManager.getConnection(int): " + e.getMessage());
                   conn = null;

               m_aNumOpenConnections[iWhichConnectionPool] ++;      // increment open connections counter
               Log.write("Opened " + m_aConnectionPoolNames[iWhichConnectionPool] + " database connection. "
                     + m_aNumOpenConnections[iWhichConnectionPool] + " "
                     + m_aConnectionPoolNames[iWhichConnectionPool]+ " connections now open.");

             return conn;

            }// end getConnection()

    Can i make this method non synchronize.

    Please elt me know the solution.

    Thank you

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
    Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now