Solved

One connection for each Thread??

Posted on 2006-07-11
8
236 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
replace a word with other 1 44
login jsp example 24 62
Configure a Bean in an XML file 4 42
hibernate example issues from command prompt 10 50
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

792 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