Guest User

L2DatabaseFactory

a guest
Jun 26th, 2016
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.87 KB | None | 0 0
  1. package com.l2jfrozen.util.database;
  2.  
  3. /*
  4. * L2jFrozen Project - www.l2jfrozen.com
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2, or (at your option)
  9. * any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  19. * 02111-1307, USA.
  20. *
  21. * http://www.gnu.org/copyleft/gpl.html
  22. */
  23.  
  24. import java.sql.Connection;
  25. import java.sql.SQLException;
  26.  
  27. import org.apache.log4j.Logger;
  28.  
  29. import com.jolbox.bonecp.BoneCPDataSource;
  30. import com.l2jfrozen.Config;
  31. import com.l2jfrozen.gameserver.thread.ThreadPoolManager;
  32.  
  33. public class L2DatabaseFactory_BoneCP extends L2DatabaseFactory
  34. {
  35. private static final Logger LOGGER = Logger.getLogger(L2DatabaseFactory_BoneCP.class);
  36.  
  37. private BoneCPDataSource _source;
  38.  
  39. public L2DatabaseFactory_BoneCP()
  40. {
  41. LOGGER.info("Initializing BoneCP [ databaseDriver -> " + Config.DATABASE_DRIVER + ", jdbcUrl -> " + Config.DATABASE_URL + ", maxConnectionsPerPartition -> " + Config.DATABASE_MAX_CONNECTIONS + ", username -> " + Config.DATABASE_LOGIN + ", password -> " + Config.DATABASE_PASSWORD + " ]");
  42.  
  43. try
  44. {
  45. if (Config.DATABASE_MAX_CONNECTIONS < 10)
  46. {
  47. Config.DATABASE_MAX_CONNECTIONS = 10;
  48. LOGGER.warn("at least " + Config.DATABASE_MAX_CONNECTIONS + " db connections are required.");
  49. }
  50.  
  51. if (Config.DATABASE_PARTITION_COUNT > 4)
  52. {
  53. Config.DATABASE_PARTITION_COUNT = 4;
  54. LOGGER.warn("max {} db connections partitions. " + Config.DATABASE_PARTITION_COUNT);
  55. }
  56.  
  57. if (Config.DATABASE_MAX_CONNECTIONS * Config.DATABASE_PARTITION_COUNT > 200)
  58. {
  59. LOGGER.warn("Max Connections number is higher then 60.. Using Partition 2 and Connection 30");
  60. Config.DATABASE_MAX_CONNECTIONS = 50;
  61. Config.DATABASE_PARTITION_COUNT = 4;
  62. }
  63.  
  64. _source = new BoneCPDataSource();
  65. // _source.setAutoCommitOnClose(true);
  66. _source.getConfig().setDefaultAutoCommit(true);
  67.  
  68. // _source.setInitialPoolSize(10);
  69. _source.getConfig().setPoolAvailabilityThreshold(10);
  70. // _source.setMinPoolSize(10);
  71. _source.getConfig().setMinConnectionsPerPartition(10);
  72. // _source.setMaxPoolSize(Config.DATABASE_MAX_CONNECTIONS);
  73. _source.getConfig().setMaxConnectionsPerPartition(Config.DATABASE_MAX_CONNECTIONS);
  74.  
  75. _source.getConfig().setPartitionCount(Config.DATABASE_PARTITION_COUNT);
  76.  
  77. _source.setAcquireRetryAttempts(0); // try to obtain connections indefinitely (0 = never quit)
  78. _source.setAcquireRetryDelayInMs(500); // 500 miliseconds wait before try to acquire connection again
  79.  
  80. // if pool is exhausted
  81. _source.setAcquireIncrement(5); // if pool is exhausted, get 5 more connections at a time
  82. // cause there is a "long" delay on acquire connection
  83. // so taking more than one connection at once will make connection pooling
  84. // more effective.
  85.  
  86. _source.setConnectionTimeoutInMs(Config.DATABASE_TIMEOUT);
  87.  
  88. // this "connection_test_table" is automatically created if not already there
  89. // _source.setAutomaticTestTable("connection_test_table");
  90. // _source.setTestConnectionOnCheckin(false);
  91.  
  92. // testing OnCheckin used with IdleConnectionTestPeriod is faster than testing on checkout
  93.  
  94. _source.setIdleConnectionTestPeriodInMinutes(1); // test idle connection every 60 sec
  95. // _source.setMaxIdleTime(1800); // 0 = idle connections never expire
  96. _source.setIdleMaxAgeInSeconds(1800);
  97. // *THANKS* to connection testing configured above
  98. // but I prefer to disconnect all connections not used
  99. // for more than 1 hour
  100.  
  101. _source.setTransactionRecoveryEnabled(true);
  102.  
  103. // enables statement caching, there is a "semi-bug" in c3p0 0.9.0 but in 0.9.0.2 and later it's fixed
  104. // _source.setMaxStatementsPerConnection(100);
  105.  
  106. // _source.setBreakAfterAcquireFailure(false); // never fail if any way possible
  107. // setting this to true will make
  108. // c3p0 "crash" and refuse to work
  109. // till restart thus making acquire
  110. // errors "FATAL" ... we don't want that
  111. // it should be possible to recover
  112. _source.setDriverClass(Config.DATABASE_DRIVER);
  113. _source.setJdbcUrl(Config.DATABASE_URL);
  114. _source.setUsername(Config.DATABASE_LOGIN);
  115. _source.setPassword(Config.DATABASE_PASSWORD);
  116.  
  117. /* Test the connection */
  118. _source.getConnection().close();
  119.  
  120. if (Config.DATABASE_DRIVER.toLowerCase().contains("microsoft"))
  121. _providerType = ProviderType.MsSql;
  122. else
  123. _providerType = ProviderType.MySql;
  124.  
  125. }
  126. catch (final Exception e)
  127. {
  128. throw new Error("L2DatabaseFactory: Failed to init database connections: " + e, e);
  129. }
  130. }
  131.  
  132. /*
  133. * private void testConnection() throws SQLException { final Connection connect = _source.getConnection(); if(connect == null) throw new SQLException(); LOGGER.finest("Connection successful"); final Statement statement = connect.createStatement(); final ResultSet resultSet =
  134. * statement.executeQuery("SHOW TABLES"); resultSet.next(); resultSet.close(); DatabaseUtils.close(statement); connect.close(); LOGGER.finest("Database Connection Working"); }
  135. */
  136.  
  137. @Override
  138. public void shutdown()
  139. {
  140. try
  141. {
  142. _source.close();
  143. }
  144. catch (final Exception e)
  145. {
  146. LOGGER.error("Error shutdowning database pool", e);
  147. }
  148. try
  149. {
  150. _source = null;
  151. }
  152. catch (final Exception e)
  153. {
  154. e.printStackTrace();
  155. }
  156. }
  157.  
  158. @Override
  159. public Connection getConnection(final boolean checkclose)
  160. {
  161. Connection con = null;
  162.  
  163. while (con == null)
  164. {
  165. try
  166. {
  167. con = _source.getConnection();
  168. if (checkclose)
  169. ThreadPoolManager.getInstance().scheduleGeneral(new ConnectionCloser(con, new RuntimeException()), Config.DATABASE_CONNECTION_TIMEOUT);
  170. }
  171. catch (final SQLException e)
  172. {
  173. LOGGER.error("L2DatabaseFactory: getConnection() failed, trying again ", e);
  174. }
  175. }
  176.  
  177. return con;
  178. }
  179.  
  180. @Override
  181. public Connection getConnection(final long max_connection_time)
  182. {
  183. Connection con = null;
  184.  
  185. while (con == null)
  186. {
  187. try
  188. {
  189. con = _source.getConnection();
  190. ThreadPoolManager.getInstance().scheduleGeneral(new ConnectionCloser(con, new RuntimeException()), max_connection_time);
  191.  
  192. }
  193. catch (final SQLException e)
  194. {
  195. LOGGER.error("L2DatabaseFactory: getConnection() failed, trying again", e);
  196. }
  197. }
  198.  
  199. return con;
  200. }
  201.  
  202. @Override
  203. public int getBusyConnectionCount()
  204. {
  205. return _source.getTotalLeased();
  206. }
  207.  
  208. }
Advertisement
Add Comment
Please, Sign In to add comment