L2DatabaseFactory.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.communityserver;
  16. import java.sql.Connection;
  17. import java.sql.SQLException;
  18. import java.util.logging.Level;
  19. import java.util.logging.Logger;
  20. import com.mchange.v2.c3p0.ComboPooledDataSource;
  21. public class L2DatabaseFactory
  22. {
  23. static Logger _log = Logger.getLogger(L2DatabaseFactory.class.getName());
  24. public static enum ProviderType
  25. {
  26. MySql,
  27. MsSql
  28. }
  29. private static L2DatabaseFactory _instance;
  30. private final ProviderType _providerType;
  31. private final ComboPooledDataSource _source;
  32. public L2DatabaseFactory() throws SQLException
  33. {
  34. try
  35. {
  36. if (Config.DATABASE_MAX_CONNECTIONS < 2)
  37. {
  38. Config.DATABASE_MAX_CONNECTIONS = 2;
  39. _log.warning("at least " + Config.DATABASE_MAX_CONNECTIONS + " db connections are required.");
  40. }
  41. _source = new ComboPooledDataSource();
  42. _source.setAutoCommitOnClose(true);
  43. _source.setInitialPoolSize(10);
  44. _source.setMinPoolSize(10);
  45. _source.setMaxPoolSize(Config.DATABASE_MAX_CONNECTIONS);
  46. _source.setAcquireRetryAttempts(0);
  47. _source.setAcquireRetryDelay(500);
  48. _source.setCheckoutTimeout(0);
  49. _source.setAcquireIncrement(5);
  50. _source.setAutomaticTestTable("connection_test_table");
  51. _source.setTestConnectionOnCheckin(false);
  52. _source.setIdleConnectionTestPeriod(3600);
  53. _source.setMaxIdleTime(0);
  54. _source.setMaxStatementsPerConnection(100);
  55. _source.setBreakAfterAcquireFailure(false);
  56. _source.setDriverClass(Config.DATABASE_DRIVER);
  57. _source.setJdbcUrl(Config.DATABASE_URL);
  58. _source.setUser(Config.DATABASE_LOGIN);
  59. _source.setPassword(Config.DATABASE_PASSWORD);
  60. _source.getConnection().close();
  61. if (Config.DATABASE_DRIVER.toLowerCase().contains("microsoft"))
  62. _providerType = ProviderType.MsSql;
  63. else
  64. _providerType = ProviderType.MySql;
  65. }
  66. catch (SQLException x)
  67. {
  68. _log.fine("Database Connection FAILED");
  69. throw x;
  70. }
  71. catch (Exception e)
  72. {
  73. _log.fine("Database Connection FAILED");
  74. throw new SQLException("could not init DB connection: " + e);
  75. }
  76. }
  77. public final String prepQuerySelect(String[] fields, String tableName, String whereClause, boolean returnOnlyTopRecord)
  78. {
  79. String msSqlTop1 = "";
  80. String mySqlTop1 = "";
  81. if (returnOnlyTopRecord)
  82. {
  83. if (getProviderType() == ProviderType.MsSql) msSqlTop1 = " Top 1 ";
  84. if (getProviderType() == ProviderType.MySql) mySqlTop1 = " Limit 1 ";
  85. }
  86. String query = "SELECT " + msSqlTop1 + safetyString(fields) + " FROM " + tableName + " WHERE " + whereClause + mySqlTop1;
  87. return query;
  88. }
  89. public final void shutdown()
  90. {
  91. try
  92. {
  93. _source.close();
  94. }
  95. catch (Exception e)
  96. {
  97. _log.log(Level.INFO, "", e);
  98. }
  99. }
  100. public final String safetyString(String[] whatToCheck)
  101. {
  102. String braceLeft = "`";
  103. String braceRight = "`";
  104. if (getProviderType() == ProviderType.MsSql)
  105. {
  106. braceLeft = "[";
  107. braceRight = "]";
  108. }
  109. String result = "";
  110. for (String word : whatToCheck)
  111. {
  112. if (result != "")
  113. result += ", ";
  114. result += braceLeft + word + braceRight;
  115. }
  116. return result;
  117. }
  118. public static final L2DatabaseFactory getInstance() throws SQLException
  119. {
  120. if (_instance == null)
  121. _instance = new L2DatabaseFactory();
  122. return _instance;
  123. }
  124. public final Connection getConnection()
  125. {
  126. Connection con = null;
  127. while (con == null)
  128. {
  129. try
  130. {
  131. con = _source.getConnection();
  132. }
  133. catch (SQLException e)
  134. {
  135. _log.warning("L2DatabaseFactory: getConnection() failed, trying again "+e);
  136. }
  137. }
  138. return con;
  139. }
  140. public final int getBusyConnectionCount() throws SQLException
  141. {
  142. return _source.getNumBusyConnectionsDefaultUser();
  143. }
  144. public final int getIdleConnectionCount() throws SQLException
  145. {
  146. return _source.getNumIdleConnectionsDefaultUser();
  147. }
  148. public final ProviderType getProviderType()
  149. {
  150. return _providerType;
  151. }
  152. }