Link to home
Start Free TrialLog in
Avatar of arctanx
arctanx

asked on

One connection for each Thread??

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.
Avatar of arctanx
arctanx

ASKER

Oh and I need ideas about closing, I guess technically it gets closed when the app terminates.
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/
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!
Avatar of arctanx

ASKER

I read the link, but how exactly would I do my DB connection?
ASKER CERTIFIED SOLUTION
Avatar of dbkruger
dbkruger

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of arctanx

ASKER

Good answers, that gets me passed my sticking point.

Thanks.
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.