Solved

One connection for each Thread??

Posted on 2006-07-11
8
227 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

705 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

19 Experts available now in Live!

Get 1:1 Help Now