0

How to handle database connection with multithreaded application. I have developed one application that created more thread. But when i run application it run correctly but after some time application is going to hang....?? what i have to do ..? How to handle Database connection with multithreaded application .?

4
  • 1
    what do you mean by "it hangs"? Where does it hang? Do you have a stack trace / heap dump when it hangs? Commented Oct 28, 2010 at 8:57
  • my mean to say my application do nothing...? there is no exception or any memory leakage..... I thing i am suffer this for database connection Commented Oct 28, 2010 at 9:06
  • What Database do you use? I know about some Memory-Leak Problems within the mySQL JDBC Driver. If your application start to slow down, it could be the memory-consumption. Commented Oct 28, 2010 at 9:06
  • yes i am using mysql JDBC driver Commented Oct 28, 2010 at 9:19

3 Answers 3

1

You'll probably would like to use a connection pool. My recommendation is c3p0.

Sign up to request clarification or add additional context in comments.

Comments

0

Database connections and threading need not be completely related.

Where are you fetching your DB connections from? Is it a central data source? Or a custom wrapper over JDBC connection? Or are you fetching it from a DB connection pool? Or are you creating a new connection in each thread?

2 Comments

thank you .. madhur Actually i am using JDBC connection . and Create single connection for all threads. can i use like this public static synchronize Connection getConnection() instead of public static Connection getConnection()...........?
No - that won't work, it still allows two threads to access the Connection at the same time. You will have to write a wrapper class around your Connection that performs DB access and synchronize the whole class on the Connection.
0

Connection in the singular? If you only have one connection then you will have to synchronize your threads access to the connection. Better to use a Database connection pool though; almost all database vendors provide a connection pool implementation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.