Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within JDBC and Relational Databases
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
Liutauras Vilda
Jeanne Boyarsky
paul wheaton
Sheriffs:
Ron McLeod
Devaka Cooray
Henry Wong
Saloon Keepers:
Tim Holloway
Stephan van Hulst
Carey Brown
Tim Moores
Mikalai Zaikin
Bartenders:
Frits Walraven
Forum:
JDBC and Relational Databases
configuration of Bonecp class file
Rd Dari
Ranch Hand
Posts: 214
I like...
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi,
How to configure perfect BoneCP class file which will help for connection in pool
my class is but it throw exception many times too many connections
package src; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.sql.PreparedStatement; import com.jolbox.bonecp.BoneCP; import com.jolbox.bonecp.BoneCPConfig; public class ConnectionManager { private static BoneCP connectionPool = null; public static void configureConnPool() { try { Class.forName("com.mysql.jdbc.Driver"); //also you need the MySQL driver BoneCPConfig config = new BoneCPConfig(); config.setJdbcUrl("jdbc:mysql://localhost:3306/facility"); config.setUsername("root"); config.setPassword("adnig"); // config.setIdleConnectionTestPeriod(60); // config.setIdleMaxAge(240); config.setMinConnectionsPerPartition(50); //if you say 5 here, there will be 10 connection available config.setMaxConnectionsPerPartition(10); config.setMaxConnectionsPerPartition(100); config.setPartitionCount(2); //2*5 = 10 connection will be available //config.setAcquireIncrement(10); //config.setStatementsCachedPerConnection(100); config.setReleaseHelperThreads(10); //config.setLazyInit(true); //depends on the application usage you should chose lazy or not //setting Lazy true means BoneCP won't open any connections before you request a one from it. connectionPool = new BoneCP(config); // setup the connection pool System.out.println("contextInitialized.....Connection Pooling is configured"); System.out.println("Total connections ==> " + connectionPool.getTotalCreatedConnections()); //System.out.println("Total Free: "+connectionPool.getTotalFree()); ConnectionManager.setConnectionPool(connectionPool); } catch (Exception e) { e.printStackTrace(); //you should use exception wrapping on real-production code } } public static void shutdownConnPool() { try { connectionPool = ConnectionManager.getConnectionPool(); System.out.println("contextDestroyed...."); if (connectionPool != null) { connectionPool.shutdown(); //this method must be called only once when the application stops. //you don't need to call it every time when you get a connection from the Connection Pool System.out.println("contextDestroyed.....Connection Pooling shut downed!"); } } catch (Exception e) { e.printStackTrace(); } } public static synchronized Connection getConnection() { Connection conn = null; try { conn = getConnectionPool().getConnection(); System.out.println("Total Free: "+connectionPool.getTotalFree()); //will get a thread-safe connection from the BoneCP connection pool. //synchronization of the method will be done inside BoneCP source } catch (Exception e) { e.printStackTrace(); } return conn; } public static void close(Statement stmt) { try { if (stmt != null) stmt.close(); } catch (Exception e) { e.printStackTrace(); } } public static void close(PreparedStatement pstmt) { try { if (pstmt != null) pstmt.close(); } catch (Exception e) { e.printStackTrace(); } } public static void close(ResultSet rSet) { try { if (rSet != null) rSet.close(); } catch (Exception e) { e.printStackTrace(); } } public static void close(Connection conn) { try { if (conn != null) conn.close(); //release the connection - the name is tricky but connection is not closed it is released //and it will stay in pool } catch (SQLException e) { e.printStackTrace(); } } public static BoneCP getConnectionPool() { return connectionPool; } public static void setConnectionPool(BoneCP connectionPool) { ConnectionManager.connectionPool = connectionPool; } }
and listner is
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package src; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; /** * Web application lifecycle listener. * @author Ading */ public class ContextListener implements ServletContextListener { public void contextInitialized(ServletContextEvent sce) { //throw new UnsupportedOperationException("Not supported yet."); ConnectionManager.configureConnPool(); } public void contextDestroyed(ServletContextEvent sce) { //throw new UnsupportedOperationException("Not supported yet."); // ConnectionManager.shutdownConnPool(); } }
Thnaks
Always look on the bright side of life. At least this ad is really tiny:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
servletcontextlistener
Connection Pooling, anyone help me please!
sl314.util.sql.ConnectionPool
How to configure Bonecp in java project without breaking connection when more than connection
Can I create connection pooling with data sources using sybase driver(jconn2.jar)?
More...