Guest User

L2databaseFactory

a guest
Jun 26th, 2016
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.69 KB | None | 0 0
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2, or (at your option)
  5. * any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  15. * 02111-1307, USA.
  16. *
  17. * http://www.gnu.org/copyleft/gpl.html
  18. */
  19. package com.equal;
  20.  
  21. import java.sql.Connection;
  22. import java.sql.SQLException;
  23. import java.util.logging.Level;
  24. import java.util.logging.Logger;
  25.  
  26. import com.mchange.v2.c3p0.ComboPooledDataSource;
  27.  
  28. public class L2DatabaseFactory
  29. {
  30. static Logger _log = Logger.getLogger(L2DatabaseFactory.class.getName());
  31.  
  32. public static enum ProviderType
  33. {
  34. MySql,
  35. MsSql
  36. }
  37.  
  38. // =========================================================
  39. // Data Field
  40. private static L2DatabaseFactory _instance;
  41. private ProviderType _providerType;
  42. private ComboPooledDataSource _source;
  43.  
  44. // =========================================================
  45. // Constructor
  46. public L2DatabaseFactory() throws SQLException
  47. {
  48. try
  49. {
  50. if (Config.DATABASE_MAX_CONNECTIONS < 2)
  51. {
  52. Config.DATABASE_MAX_CONNECTIONS = 2;
  53. _log.warning("at least " + Config.DATABASE_MAX_CONNECTIONS + " db connections are required.");
  54. }
  55.  
  56. _source = new ComboPooledDataSource();
  57. _source.setAutoCommitOnClose(true);
  58.  
  59. _source.setInitialPoolSize(10);
  60. _source.setMinPoolSize(10);
  61. _source.setMaxPoolSize(Config.DATABASE_MAX_CONNECTIONS);
  62.  
  63.  
  64. _source.setAcquireRetryAttempts(0); // try to obtain connections indefinitely (0 = never quit)
  65. _source.setAcquireRetryDelay(500); // 500 miliseconds wait before try to acquire connection again
  66. _source.setCheckoutTimeout(0); // 0 = wait indefinitely for new connection
  67. // if pool is exhausted
  68. _source.setAcquireIncrement(5); // if pool is exhausted, get 5 more connections at a time
  69. // cause there is a "long" delay on acquire connection
  70. // so taking more than one connection at once will make connection pooling
  71. // more effective.
  72.  
  73. // this "connection_test_table" is automatically created if not already there
  74. _source.setAutomaticTestTable("connection_test_table");
  75. _source.setTestConnectionOnCheckin(false);
  76.  
  77. // testing OnCheckin used with IdleConnectionTestPeriod is faster than testing on checkout
  78.  
  79. _source.setIdleConnectionTestPeriod(3600); // test idle connection every 60 sec
  80. _source.setMaxIdleTime(0); // 0 = idle connections never expire
  81. // *THANKS* to connection testing configured above
  82. // but I prefer to disconnect all connections not used
  83. // for more than 1 hour
  84.  
  85. // enables statement caching, there is a "semi-bug" in c3p0 0.9.0 but in 0.9.0.2 and later it's fixed
  86. _source.setMaxStatementsPerConnection(100);
  87.  
  88. _source.setBreakAfterAcquireFailure(false); // never fail if any way possible
  89. // setting this to true will make
  90. // c3p0 "crash" and refuse to work
  91. // till restart thus making acquire
  92. // errors "FATAL" ... we don't want that
  93. // it should be possible to recover
  94. _source.setDriverClass(Config.DATABASE_DRIVER);
  95. _source.setJdbcUrl(Config.DATABASE_URL);
  96. _source.setUser(Config.DATABASE_LOGIN);
  97. _source.setPassword(Config.DATABASE_PASSWORD);
  98.  
  99. /* Test the connection */
  100. _source.getConnection().close();
  101.  
  102. if (Config.DEBUG) _log.fine("Database Connection Working");
  103.  
  104. if (Config.DATABASE_DRIVER.toLowerCase().contains("microsoft"))
  105. _providerType = ProviderType.MsSql;
  106. else
  107. _providerType = ProviderType.MySql;
  108. }
  109. catch (SQLException x)
  110. {
  111. if (Config.DEBUG) _log.fine("Database Connection FAILED");
  112. // rethrow the exception
  113. throw x;
  114. }
  115. catch (Exception e)
  116. {
  117. if (Config.DEBUG) _log.fine("Database Connection FAILED");
  118. throw new SQLException("could not init DB connection:"+e);
  119. }
  120. }
  121.  
  122. // =========================================================
  123. // Method - Public
  124. public final String prepQuerySelect(String[] fields, String tableName, String whereClause, boolean returnOnlyTopRecord)
  125. {
  126. String msSqlTop1 = "";
  127. String mySqlTop1 = "";
  128. if (returnOnlyTopRecord)
  129. {
  130. if (getProviderType() == ProviderType.MsSql) msSqlTop1 = " Top 1 ";
  131. if (getProviderType() == ProviderType.MySql) mySqlTop1 = " Limit 1 ";
  132. }
  133. String query = "SELECT " + msSqlTop1 + safetyString(fields) + " FROM " + tableName + " WHERE " + whereClause + mySqlTop1;
  134. return query;
  135. }
  136.  
  137. public void shutdown()
  138. {
  139. try {
  140. _source.close();
  141. } catch (Exception e) {_log.log(Level.INFO, "", e);}
  142. try {
  143. _source = null;
  144. } catch (Exception e) {_log.log(Level.INFO, "", e);}
  145. }
  146.  
  147. public final String safetyString(String[] whatToCheck)
  148. {
  149. // NOTE: Use brace as a safty percaution just incase name is a reserved word
  150. String braceLeft = "`";
  151. String braceRight = "`";
  152. if (getProviderType() == ProviderType.MsSql)
  153. {
  154. braceLeft = "[";
  155. braceRight = "]";
  156. }
  157.  
  158. String result = "";
  159. for(String word : whatToCheck)
  160. {
  161. if(result != "") result += ", ";
  162. result += braceLeft + word + braceRight;
  163. }
  164. return result;
  165. }
  166.  
  167. // =========================================================
  168. // Property - Public
  169. public static L2DatabaseFactory getInstance() throws SQLException
  170. {
  171. if (_instance == null)
  172. {
  173. _instance = new L2DatabaseFactory();
  174. }
  175. return _instance;
  176. }
  177.  
  178. public Connection getConnection() //throws SQLException
  179. {
  180. Connection con=null;
  181.  
  182. while(con==null)
  183. {
  184. try
  185. {
  186. con=_source.getConnection();
  187. } catch (SQLException e)
  188. {
  189. _log.warning("L2DatabaseFactory: getConnection() failed, trying again "+e);
  190. }
  191. }
  192. return con;
  193. }
  194.  
  195. public int getBusyConnectionCount() throws SQLException
  196. {
  197. return _source.getNumBusyConnectionsDefaultUser();
  198. }
  199.  
  200. public int getIdleConnectionCount() throws SQLException
  201. {
  202. return _source.getNumIdleConnectionsDefaultUser();
  203. }
  204.  
  205. public final ProviderType getProviderType() { return _providerType; }
  206. }
Advertisement
Add Comment
Please, Sign In to add comment