JDCConnectionPool.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. namespace System.Data.Common
  2. {
  3. using java.sql;
  4. using java.util;
  5. public class JDCConnectionPool
  6. {
  7. private ArrayList _connections;
  8. private String _url, _user, _password;
  9. readonly private long timeout = 10000;
  10. readonly private int _initPoolsize = 10;
  11. private int _maxPoolSize = 100;
  12. private static bool _shutdown = false;
  13. public JDCConnectionPool(String url, String user, String password)
  14. {
  15. _url = url;
  16. _user = user;
  17. _password = password;
  18. _connections = new ArrayList(_initPoolsize);
  19. }
  20. public void closeConnections()
  21. {
  22. lock(this)
  23. {
  24. if(_connections.size() > 0)
  25. {
  26. Iterator connlist = _connections.iterator();
  27. while (connlist.hasNext())
  28. {
  29. JDCConnection conn = (JDCConnection) connlist.next();
  30. removeConnection(conn);
  31. }
  32. }
  33. }
  34. }
  35. private void removeConnection(JDCConnection conn)
  36. {
  37. lock(this)
  38. {
  39. _connections.remove(conn);
  40. }
  41. }
  42. public Connection getConnection() //throws SQLException
  43. {
  44. lock(this)
  45. {
  46. JDCConnection c;
  47. for (int i = 0; i < _connections.size(); i++)
  48. {
  49. c = (JDCConnection) _connections.get(i);
  50. if (c.lease())
  51. return c;
  52. }
  53. if(_connections.size() < _maxPoolSize)
  54. {
  55. Connection conn = DriverManager.getConnection(_url, _user, _password);
  56. c = new JDCConnection(conn, this);
  57. c.lease();
  58. _connections.add(c);
  59. return c;
  60. }
  61. return null;
  62. }
  63. }
  64. public void returnConnection(JDCConnection conn)
  65. {
  66. lock(this)
  67. {
  68. conn.expireLease();
  69. }
  70. }
  71. }
  72. }