Classic Singleton mode c3p0 to control database connection pooling

Source: Internet
Author: User
Tags connection pooling

  1. Package com.c3p0.datapools;

  2. Database Connection Pool Singleton mode

  3. Import java.sql.Connection;

  4. Import java.sql.SQLException;

  5. Import Com.mchange.v2.c3p0.ComboPooledDataSource;

  6. Import com.mchange.v2.c3p0.DataSources;

  7. Public final class ConnectionManager {

  8. private static ConnectionManager instance;

  9. Private Combopooleddatasource DS;

  10. Private ConnectionManager () throws Exception {

  11. ds = new Combopooleddatasource ();

  12. Ds.setdriverclass ("Oracle.jdbc.driver.OracleDriver");

  13. Ds.setjdbcurl ("JDBC:ORACLE:THIN:@127.0.0.1:1521:ORCL");

  14. Ds.setuser ("Scott");

  15. Ds.setpassword ("Tiger");

  16. //Initialize to get three connections, the value should be between Minpoolsize and Maxpoolsize. Default:3 initialpoolsize

  17. Ds.setinitialpoolsize (3);

  18. The maximum number of connections that are kept in the connection pool. Default:15 maxpoolsize

  19. Ds.setmaxpoolsize (100);

  20. The minimum number of connections that are kept in the connection pool.

  21. Ds.setminpoolsize (1);

  22. When the connection in the connection pool runs out, c3p0 the number of connections that are fetched at one time. Default:3 acquireincrement

  23. Ds.setacquireincrement (10);

  24. Check for idle connections in all connection pools every 60 seconds. default:0 Idleconnectiontestperiod

  25. Ds.setidleconnectiontestperiod (60);

  26. Maximum idle time, unused in 25,000 seconds, the connection is discarded. If 0, it will never be discarded. default:0 MaxIdleTime

  27. Ds.setmaxidletime (25000);

  28. All uncommitted operations are rolled back by default when the connection is closed. Default:false Autocommitonclose

  29. Ds.setautocommitonclose (TRUE);

  30. Defines test statements that are executed by all connection tests. This one significantly improves the test speed in the case of connection testing. Attention:

  31. The test table must exist at the time of the initial data source. Default:null Preferredtestquery

  32. Ds.setpreferredtestquery ("Select Sysdate from Dual");

  33. Please use it only when you need it because of high performance consumption. If set to true then each connection commits the

  34. Officer the validity of the test. It is recommended to use Idleconnectiontestperiod or automatictesttable

  35. and other methods to improve the performance of the connection test. Default:false Testconnectiononcheckout

  36. Ds.settestconnectiononcheckout (TRUE);

  37. If set to true then the validity of the officer connection is obtained while the connection is made. Default:false Testconnectiononcheckin

  38. Ds.settestconnectiononcheckin (TRUE);

  39. Defines the number of repeated attempts to obtain a new connection from the database after a failure. Default:30 acquireretryattempts

  40. Ds.setacquireretryattempts (30);

  41. Interval in two connections, in milliseconds. default:1000 Acquireretrydelay

  42. Ds.setacquireretrydelay (1000);

  43. Getting a connection failure will cause any thread that waits for the connection pool to get the connection to throw an exception. But the data source is still valid

  44. Reserved and continue trying to get the connection the next time you call Getconnection (). If set to true, then try to

  45. When a connection failure is obtained, the data source declares that it has been fractured and closed permanently. Default:false breakafteracquirefailure

  46. Ds.setbreakafteracquirefailure (TRUE);

  47. <!--the time when the client calls getconnection () after the connection pool has run out, and then throws

  48. SQLException, if set to 0, waits indefinitely. Unit milliseconds. Default:0--

  49. <property name= "Checkouttimeout" >100</property>

  50. <!--C3P0 will build a blank table named Test and test it with its own query statement. If this parameter is defined, then

  51. Property Preferredtestquery will be ignored. You cannot do anything on this test sheet, it will be for C3P0 testing only

  52. Use. Default:null-->

  53. <property name= "Automatictesttable" >Test</property>

  54. <!--the standard parameters of JDBC to control the number of preparedstatements loaded within the data source. But due to the statements of the pre-cache

  55. belong to a single connection instead of the entire connection pool. So setting this parameter takes into account a variety of factors.

  56. If both maxstatements and maxstatementsperconnection are 0, the cache is closed. Default:0-->

  57. <property name= "Maxstatements" >100</property>

  58. <!--maxstatementsperconnection defines the maximum number of cache statements that a single connection in a connection pool has. Default:0--

  59. <property name= "Maxstatementsperconnection" ></property>

  60. <!--C3P0 is asynchronous, and slow JDBC operations are done through the help process. Extending these operations can effectively improve performance

  61. Multiple operations are performed at the same time through multithreading. Default:3-->

  62. <property name= "Numhelperthreads" >3</property>

  63. <!--the user to wait up to 300 seconds before modifying the system configuration parameters. DEFAULT:300--

  64. <property name= "Propertycycle" >300</property>

  65. }

  66. public static final ConnectionManager getinstance () {

  67. if (instance = = null) {

  68. try {

  69. Instance = new ConnectionManager ();

  70. } catch (Exception e) {

  71. E.printstacktrace ();

  72. }

  73. }

  74. return instance;

  75. }

  76. Public synchronized final Connection getconnection () {

  77. try {

  78. return Ds.getconnection ();

  79. } catch (SQLException e) {

  80. E.printstacktrace ();

  81. }

  82. return null;

  83. }

  84. protected void Finalize () throws Throwable {

  85. Datasources.destroy (DS); Close DataSource

  86. Super.finalize ();

  87. }

  88. }

Classic Singleton mode c3p0 to control database connection pooling

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.