Solved

One connection for each Thread??

Posted on 2006-07-11
8
230 Views
Last Modified: 2012-05-05
I can't post all of my code, but here is the basic layout (well, the important stuff):

I've got a Threaded app, that calls a new instance of a class, basically, well, forever.

while (running)
{
    // poll a database table, check some values
    if (CONDITION CHECKS OUT)
    {
        new Test().start(); // <-- do some db transactions in here
    }
}

So...I could possibly have 10 or so Threads going at once...maybe more.

Now here is now I'm doing my connection to the database:

private static Connection conn = null;

public static Connection getConn()
{
    if (conn == null)
    {
        try
        {
            DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
            setConn(DriverManager.getConnection(dbUrl,dbUid,dbPwd));
        }
        catch(Exception e)
        {
            logger.error("Get Connection failed: " + e.getMessage());
        }
    }
    return conn;
}

public static void setConn(Connection conn)
{
    logger.debug("Setting new connection...");
    FlashClient.conn = conn;
}

Basically I get the connection ONE TIME, and keep it, so whenever the Thread touches the db, it uses the connection that I created above....

Now someone was telling me EACH THREAD needs to have it's own connection to do work.

TRUE OR FALSE???

And isn't that alot of overhead?  Opening a new connection, closing it...opening another one, closing it...blah blah blah??

What should I do???

I know nothing about Thread pooling..and I don't think that's an option for me.
0
Comment
Question by:arctanx
  • 4
  • 3
8 Comments
 
LVL 2

Author Comment

by:arctanx
ID: 17085540
Oh and I need ideas about closing, I guess technically it gets closed when the app terminates.
0
 
LVL 11

Expert Comment

by:dbkruger
ID: 17085593
Database connections are synchronous. While you are using one, it's busy. You do not want to arbitrarily keep increasing the number of active threads. Beyond a certain point, that just degrades the databases' performance (just when you need it most). What you want is a thread pool, a class that manages connections for you and reuses them when they com available.
http://jakarta.apache.org/commons/sandbox/threadpool/
0
 
LVL 11

Expert Comment

by:dbkruger
ID: 17085598
I see that you say at the end that you don't think thread pooling is for you. But it is, whether you like it or not. If you don't use someone else's thread pool, you'll be writing your own without (as you say) knowing anything about it. Use jakarta commons and be grateful it's there!
0
 
LVL 2

Author Comment

by:arctanx
ID: 17085665
I read the link, but how exactly would I do my DB connection?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 11

Accepted Solution

by:
dbkruger earned 70 total points
ID: 17086017
Sorry, you want the database pool, not the thread pool.
http://jakarta.apache.org/commons/dbcp/
0
 
LVL 4

Assisted Solution

by:astorer
astorer earned 30 total points
ID: 17086264
It depends on the amount of database access from your Threads.  A database connection pool is an overhead if each thread is going to rarely access the database.  But if you are accessing the database lots, then a DB connection pool is the way to go.

Note that having a single connection as you have it now is perfectly fine for low volume requests.  But you should be careful to synchronize access to the database really to prevent any mishaps (that synchronization may be looked after for you by the JDBC driver - it depends.  Better to synchronize it yourself just to be sure).


0
 
LVL 2

Author Comment

by:arctanx
ID: 17086436
Good answers, that gets me passed my sticking point.

Thanks.
0
 
LVL 11

Expert Comment

by:dbkruger
ID: 17090481
I believe astorer is incorrect. The overhead of a connection pool is negligable. You have a bunch of threads that have to share connections. If you don't need a lot of performance, and/or don't want to impact the server by logging in multiple times, then just allocate a DB pool of size 1, and you will only be servicing one at a time. You still need the synchronization to prevent multiple threads from trying to use the same connection at the same time, and the only thing writing it for  yourself will do is a) take more of your time and b) possibly get it wrong for a while, since synchronizing is always tricky stuff.  Whatever you wrote would be equivalent to the synchronization part of the DB pool.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 86
thymeleaf natural templating vs JSP 2 68
web services creation SOAP vs REST 5 20
reverse digits of a number using for loop 5 36
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.

920 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

15 Experts available now in Live!

Get 1:1 Help Now