|
|
| connection pooling |
Expert:mamatha
|
| Answers |
Hi friend,<br /><br /><br />Connection Pooling :<br /><br />A connection pool is a cache of database connections maintained by the database. <br />This results connections can be reused when the database receives future requests for data.<br />It is used to increase the performance of executing commands on a database.<br />It open and maintain a database connection for each user.<br /><br />In JDBC connection pool, a pool of Connection objects is created at the time the application server (or some other server) starts.<br />These objects are then managed by a pool manager.When the connection pool server starts, it creates a predetermined <br />number of Connection objects.A client application would then perform a JNDI lookup to retrieve a reference to a <br />DataSource object that implements the ConnectionPoolDataSource interface. The ConnectionPoolDataSource would return a <br />Connection object that implemented the PooledConnection interface.<br /><br />For example :<br /><br />import java.sql.*;<br />import java.util.Properties;<br />import org.apache.commons.dbcp.*;<br />import org.apache.commons.pool.impl.*;<br />import org.apache.commons.pool.KeyedObjectPoolFactory;<br /><br /><br />public class JConPool {<br /><br /> public static void main(String args[]) throws Exception {<br /><br /> GenericObjectPool gObjPool = new GenericObjectPool();<br /><br /> Properties pros = new Properties();<br /> pros.setProperty("Username", "root");<br /> pros.setProperty("Password", "");<br /> ConnectionFactory cf = new DriverConnectionFactory(new com.mysql.jdbc.Driver(),"jdbc:<a href="mysql://localhost/commons",pros" target="_blank">mysql://localhost/commons",pros</a>);<br /><br /><br /> KeyedObjectPoolFactory kopf =new GenericKeyedObjectPoolFactory(null, 8);<br /><br /> PoolableConnectionFactory pcf = new PoolableConnectionFactory(cf,gObjPool,kopf, null, false, true);<br /><br /><br /> for(int i = 0; i < 5; i++) {<br /> gObjPool.addObject();<br /> }<br /><br /> <br /> PoolingDriver poold = new PoolingDriver();<br /> poold.registerPool("example", gObjPool);<br /><br /> for(int i = 0; i < 5; i++) {<br /> gObjPool.addObject();<br /> }<br /><br /> Connection connection = java.sql.DriverManager.getConnection("jdbc:apache:commons:dbcp:example");<br /><br /> System.err.println("Connection: " + connection ); <br /> PreparedStatement ps = connection.prepareStatement("Select * from tablename where id = ?");<br /><br /> System.err.println("Active: " + gObjPool.getNumActive() + ", Idle: " + gObjPool.getNumIdle());<br /><br /> connection.close();<br /><br /> System.err.println("Active: " + gObjPool.getNumActive() + ", Idle: " + gObjPool.getNumIdle());<br /><br /> }<br />}<br /><br />Thanks
|
| More Questions |
|
|
Post Answers
Ask Question
Facing Programming Problem?
|
|
|
|
|