I used c3p0
pooling almost everywhere. After reading few articles and suggestions I want to
try BoneCP to see how its workout.
Here is basic
sample how to test it.
You must
have below jar files in your classpath
bonecp-0.8.0.RELEASE.jar
guava-15.0.jar
slf4j-api-1.7.5.jar
mysql-connector-java-5.0.8-bin
DB driver
My case its
Mysql. So I use below link to download.
You can try
these on below links.
http://jolbox.com/ - Go to download link
http://code.google.com/p/guava-libraries/
- Google guava lib
http://www.slf4j.org/download.html
- slf4j api jar only
Once the
setup is done its quite straightforward. You can try piece of code appear below.
------------------------------------------------------------------
oneCP connectionPool = null;
Connection connection = null;
try {
// load the database driver (make
sure this is in your classpath!)
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception e) {
e.printStackTrace();
return;
}
try {
// setup the connection pool
BoneCPConfig config = new
BoneCPConfig();
config.setJdbcUrl("jdbc:mysql://10.82.100.61/kasun"); // jdbc
url specific to your database, eg jdbc:mysql://127.0.0.1/db
config.setUsername("kasun");
config.setPassword("kasun123");
config.setMinConnectionsPerPartition(5);
config.setMaxConnectionsPerPartition(10);
config.setPartitionCount(1);
connectionPool = new
BoneCP(config); // setup the connection pool
connection =
connectionPool.getConnection(); // fetch a connection
if (connection != null) {
System.out.println("Connection successful!");
Statement stmt =
connection.createStatement();
ResultSet rs =
stmt.executeQuery("SELECT * FROM t1"); // do something with the
connection.
while (rs.next()) {
System.out.println("Column One - " + rs.getString(1));
System.out.println("Column Two - " + rs.getString(2));
}
}
Thread.sleep(30000);
connectionPool.shutdown(); //
shutdown connection pool.
} catch (SQLException e) {
e.printStackTrace();
} catch (InterruptedException iex) {
iex.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
------------------------------------------------------------------
Thread sleep is introduce in order to check the connectivity from Mysql.
Table definition as below.
CREATE TABLE `t1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`address` varchar(30) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3
DEFAULT CHARSET=latin1
Table data
as below
mysql> select * from t1 \G
*************************** 1. row
***************************
id: 2
address: bandarawela
1 row in set (0.00 sec)
Compile it
and run.
SLF4J: Failed to load class
"org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation
(NOP) logger implementation
SLF4J: See
http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Connection successful!
Column One - 2
Column Two – bandarawela
Below is the connections listed in Mysql.
mysql> show processlist;
+-----+-------+-----------------------------+-------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+-----+-------+-----------------------------+-------+---------+------+-------+------------------+
| 214 | kasun | MA02ENLKASUN:50805 | kasun | Query | 0 | init | show processlist |
| 231 | kasun | MA02ENLKASUN:52283 | kasun | Sleep | 7 | | NULL |
| 232 | kasun | MA02ENLKASUN:52284 | kasun | Sleep | 7 | | NULL |
| 233 | kasun | MA02ENLKASUN:52285 | kasun | Sleep | 7 | | NULL |
| 234 | kasun | MA02ENLKASUN:52286 | kasun | Sleep | 7 | | NULL |
| 235 | kasun | MA02ENLKASUN:52287 | kasun | Sleep | 7 | | NULL |
+-----+-------+-----------------------------+-------+---------+------+-------+------------------+
6 rows in set (0.00 sec)
Below is the connections listed in Mysql.
mysql> show processlist;
+-----+-------+-----------------------------+-------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+-----+-------+-----------------------------+-------+---------+------+-------+------------------+
| 214 | kasun | MA02ENLKASUN:50805 | kasun | Query | 0 | init | show processlist |
| 231 | kasun | MA02ENLKASUN:52283 | kasun | Sleep | 7 | | NULL |
| 232 | kasun | MA02ENLKASUN:52284 | kasun | Sleep | 7 | | NULL |
| 233 | kasun | MA02ENLKASUN:52285 | kasun | Sleep | 7 | | NULL |
| 234 | kasun | MA02ENLKASUN:52286 | kasun | Sleep | 7 | | NULL |
| 235 | kasun | MA02ENLKASUN:52287 | kasun | Sleep | 7 | | NULL |
+-----+-------+-----------------------------+-------+---------+------+-------+------------------+
6 rows in set (0.00 sec)
BoneCP
simple and easy to user. You can check their website for further and for more examples.
Worth a try.
No comments:
Post a Comment