A basic Mature database connection pool (part 1)

Source: Internet
Author: User

Recently, I started to develop a website with a powerful background. When using the connection pool, I felt that the use of the server's own connection pool was always limited. At the same time, in order to deepen the learning and research of Java. Write down the following connection pool class.
This connection pool provides the following functions;

1) initialize once and use it everywhere.
2) the powerful log function records every SQL action, including Connection, ResultSet, and Statement.
3) based on the number of connections, connections that have been released or timed out are automatically recycled.
4) flexible configuration. You can use a variety of JDBC drivers and support multiple drivers.

Update description:
1) added character set configuration items.
2) A debugging switch is added to facilitate debugging before use.
3) changed the Log date output format.
4) eliminate a bug in createStatement l (thanks to rouselion's usage feedback)
5) fixed the potential problem of the getXXX function of the encapsulated class.

Source code:

  1. // File: ***** <ConnectionManager. java> ****
  2. /*
  3. * @ Title connection pool
  4. * @ Author: zxg
  5. * @ Version 2.5
  6. * @ Memo: defines database connections and database connection pools.
  7. */
  8. PackageCom. dbaccess. dbpool;
  9. ImportJava. io .*;
  10. ImportJava. SQL .*;
  11. ImportJava. util .*;
  12. ImportCom. mysql. jdbc. Driver;
  13. Public ClassConnectionManager {
  14. Static PrivateConnectionManager instance;// Unique instance
  15. // Static private int clients;
  16. Static Private LongCheckperiod = 0;
  17. Private VectorDrivers =New Vector();
  18. Private HashtablePools =New Hashtable();
  19. Private TimerCheckConnTimer =New Timer();
  20. // Debug mode switch
  21. Private Static BooleanDebug =False;
  22. // Character Set Hasse table
  23. Static Private HashtableCharacters =New Hashtable();
  24. Static Private PrintWriterLog;
  25. /**
  26. * A unique instance is returned. If this method is called for the first time, an instance is created.
  27. *
  28. * @ Return ConnectionManager: unique instance
  29. */
  30. Static Synchronized PublicConnectionManager getInstance (){
  31. If(Instance =Null){
  32. Instance =NewConnectionManager ();
  33. }
  34. // Clients ++;
  35. ReturnInstance;
  36. }
  37. /**
  38. * The construction function is private to prevent other objects from creating instances of this class.
  39. */
  40. PrivateConnectionManager (){
  41. Init ();
  42. }
  43. /**
  44. * The read attribute is initialized.
  45. */
  46. Private VoidInit (){
  47. PropertiesDbProps =Null;
  48. Try{
  49. InputStreamIs = getClass (). getResourceAsStream ("db. properties ");
  50. DbProps =New Properties();
  51. DbProps. load (is );
  52. }
  53. Catch(ExceptionE ){
  54. E. printStackTrace ();
  55. System. Err. println ("attribute file cannot be read =" +
  56. "Make sure that db. properties is in the path specified by CLASSPATH ");
  57. Return;

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.