How To Develop A Standalone Relationship Pool In Java?

0
8
Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  


Following is the strategy I frequently desire to build the connection pool for any databases. I use the BasicDataSource course produced by “Apache Software Foundataion” and out there for free. For doing work with this, you could have to have to include these jar documents to your classpath:

1. commons-dbcp.jar
2. commons-pooljar
3. commons-collections.jar
4. mysql-connector-java-5.1.5-bin.jar (for MySQL database server. Substitute this with courses12.jar if you are utilizing Oracle)

To develop a DataSource object, just instanciate the BasicDataSource class and established the properties similar to the database.

BasicDataSource ds = new BasicDataSource()
ds.setDriverClassName("com.mysql.jdbc.Driver")
ds.setUsername("root")
ds.setPassword("solution")
ds.setUrl("jdbc:mysql://localhost/vinod")

Change the word “localhost” with the IP handle of the remote server, if you are using one particular.

You can now set the connection-pool attributes also.

ds.setMaxActive(20)
ds.setMaxIdle(2)

That’s it. You have just designed 20 ready to use connection objects in a pool.

Commonly you wrap all these statements in a process as beneath:

DataSource getDataSource()
BasicDataSource ds = new BasicDataSource()
ds.setDriverClassName("com.mysql.jdbc.Driver")
ds.setUsername("root")
ds.setPassword("magic formula")
ds.setUrl("jdbc:mysql://localhost/vinod")
ds.setMaxActive(20)
ds.setMaxIdle(2)
return ds

All you have to do to retrieve this is to call the getConnection() process of javax.sql.DataSource.

DataSource ds=getDataSource()
Connection conn=ds.getConnection()

Really don’t forget to near the link, as this would guarantee your link is returned to the pool.

Down load sample plan from [http://kvinod.com/standalone-dbcp-in-java/StandaloneDBCP.java]

At the time you have the connection, you can use the exact to produce Assertion, PreparedStatement or use it for transaction administration etc.

Source by Vinod K Kayartaya

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

LEAVE A REPLY

Please enter your comment!
Please enter your name here