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 .?
You'll probably would like to use a connection pool. My recommendation is c3p0.
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?
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.
Related
Until now, whenever I query the database I open a fresh connection to the database. How do I implement the property that once I open the connection I can reuse that?
With this done, please tell me if I could leak the resources.
Basically you need JDBC connection pool, typically implementing DataSource interface. Have a look at dbcp and c3p0. Chances are you container/server already provides an implementation of connection pooling.
When you use a connection pool every time you open a connection you are actually taking one from the pool (or opening if pool is empty). When closing the connection, it is actually returned to the pool. The leak can only occur if you forget the latter. (or forget closing ResultSet, Statement...)
You can (and should) reuse db connections. Connection pooling is one of the techniques for this. A thorough tutorial on connection pooling can be read here : http://java.sun.com/developer/onlineTraining/Programming/JDCBook/conpool.html
If the server running Oracle database reboots, this probably should invalidate all previous JDBC connections in the connection pool (running as part of application on another server).
Some connection pools can handle this, others require manual re-initialization, about Oracle I do not know. Would Oracle connection pool (versions 11g and up) be able to detect such situation and recover, or do I need to check for this myself and reinitialize?
Ideally, I would like to know also that would happen for the connection in use that has been borrowed from the pool. Would such connection proxy be able to use the rebooted server?
I have read http://docs.oracle.com/cd/B10501_01/java.920/a96654/connpoca.htm without particular success.
I have MySql server. I am making trivial CRUD calls to it from java servlets. I am establishing connection before each call and closing it at the end.
What should be ideal way to handle this scenario so the application could scale and handle user load decently.
You should maintain a connection pool which recycles recently used connections. That way, each request to the mysql server doesn't require connection setup.
definately the way to go is using connection pools (Connection pooling is a technique used for sharing database connections among requesting clients). Connection pooling provide significant benefits in terms of application performance, concurrency and scalability and usually involves little or no code modification.
Its pretty forward to use connection pools using tomcat but usually they differ in implementation details from version to version.
I have code that connects to a SQL DB and queries it based on user input. I would like to connect using a Pool instead to speed up the query times. I attempted to write a Pool and Manager class but I am getting an unreported NamingException when I try to get a connection from the pool. I have also already caught NamingExceptions in my getConnection() function.
Does anyone know why I am getting this error?
Or could point me in the right direction to create a valid ConnectionPool?
You should use the latest JDBC driver, which supports connection pooling internal, so you don't have to write your own.
You have 3 options:
If your code runs in an application server, configure a connection pool in the server.
Some JDBC drivers provide a connection pool implementation (but many only provide a poolable data source that is meant for integration with an app server).
Use commons-dbcp or some equivalent library to create a connection pool.
I have a stand-alone Java windows application developed based on Swing. It connects to a MySQL database for data storage. In case the database connection fails, I am getting a link failure exception from the MySQL JDBC driver (MySQLNonTransientConnectionException). I don't want to re-instantiate my database connection object or the whole program in case such a link failure issue happens. I just want to tell the user to try again later without having to restart the entire application. If the user is asked to restart the entire application, that would probably give a negative impression on the quality of the program. What do you think would be the preferred way for a standard java application to fail-over after such a database link failure without having to re-instantiate all the communication objects? Thanks in advance.
Use a Connection Pool (such as C3PO or DBCP). Your application takes the Connections from the pool, executes the statement(s) and puts the Connection back into the pool. The pool can be configured to test the JDBC Connections. For example, if they become stale, they can be automatically reinstantiated by the pool.
If your application takes the Connection from the pool, it will be a valid Connection. Let the pool handle the management of valid/invalid/stale JDBC Connections.