Solved

One connection for each Thread??

Posted on 2006-07-11
8
233 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 2

Author Comment

by:arctanx
ID: 17085665
I read the link, but how exactly would I do my DB connection?
0
 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

776 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