123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- package com.l2jserver;
- import java.sql.Connection;
- import java.sql.SQLException;
- import java.util.concurrent.Executors;
- import java.util.concurrent.ScheduledExecutorService;
- import java.util.concurrent.TimeUnit;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import com.l2jserver.gameserver.ThreadPoolManager;
- import com.mchange.v2.c3p0.ComboPooledDataSource;
- public class L2DatabaseFactory
- {
- private static final Logger _log = Logger.getLogger(L2DatabaseFactory.class.getName());
-
- private static L2DatabaseFactory _instance;
- private static volatile ScheduledExecutorService _executor;
- private ComboPooledDataSource _source;
-
-
- public L2DatabaseFactory() throws SQLException
- {
- try
- {
- if (Config.DATABASE_MAX_CONNECTIONS < 2)
- {
- Config.DATABASE_MAX_CONNECTIONS = 2;
- _log.warning("A minimum of " + Config.DATABASE_MAX_CONNECTIONS + " db connections are required.");
- }
-
- _source = new ComboPooledDataSource();
- _source.setAutoCommitOnClose(true);
-
- _source.setInitialPoolSize(10);
- _source.setMinPoolSize(10);
- _source.setMaxPoolSize(Math.max(10, Config.DATABASE_MAX_CONNECTIONS));
-
- _source.setAcquireRetryAttempts(0);
- _source.setAcquireRetryDelay(500);
- _source.setCheckoutTimeout(0);
-
- _source.setAcquireIncrement(5);
-
-
-
-
-
- _source.setAutomaticTestTable("connection_test_table");
- _source.setTestConnectionOnCheckin(false);
-
-
-
- _source.setIdleConnectionTestPeriod(3600);
- _source.setMaxIdleTime(Config.DATABASE_MAX_IDLE_TIME);
-
-
-
-
-
- _source.setMaxStatementsPerConnection(100);
-
- _source.setBreakAfterAcquireFailure(false);
-
-
-
-
-
- _source.setDriverClass(Config.DATABASE_DRIVER);
- _source.setJdbcUrl(Config.DATABASE_URL);
- _source.setUser(Config.DATABASE_LOGIN);
- _source.setPassword(Config.DATABASE_PASSWORD);
-
-
- _source.getConnection().close();
-
- if (Config.DEBUG)
- {
- _log.fine("Database Connection Working");
- }
- }
- catch (SQLException x)
- {
- if (Config.DEBUG)
- {
- _log.fine("Database Connection FAILED");
- }
-
- throw x;
- }
- catch (Exception e)
- {
- if (Config.DEBUG)
- {
- _log.fine("Database Connection FAILED");
- }
- throw new SQLException("Could not init DB connection:" + e.getMessage());
- }
- }
-
-
- public void shutdown()
- {
- try
- {
- _source.close();
- }
- catch (Exception e)
- {
- _log.log(Level.INFO, "", e);
- }
- try
- {
- _source = null;
- }
- catch (Exception e)
- {
- _log.log(Level.INFO, "", e);
- }
- }
-
-
- public static L2DatabaseFactory getInstance() throws SQLException
- {
- synchronized (L2DatabaseFactory.class)
- {
- if (_instance == null)
- {
- _instance = new L2DatabaseFactory();
- }
- }
- return _instance;
- }
-
-
- public Connection getConnection()
- {
- Connection con = null;
- while (con == null)
- {
- try
- {
- con = _source.getConnection();
- if (Server.serverMode == Server.MODE_GAMESERVER)
- {
- ThreadPoolManager.getInstance().scheduleGeneral(new ConnectionCloser(con, new RuntimeException()), Config.CONNECTION_CLOSE_TIME);
- }
- else
- {
- getExecutor().schedule(new ConnectionCloser(con, new RuntimeException()), Config.CONNECTION_CLOSE_TIME, TimeUnit.MILLISECONDS);
- }
- }
- catch (SQLException e)
- {
- _log.log(Level.WARNING, "L2DatabaseFactory: getConnection() failed, trying again " + e.getMessage(), e);
- }
- }
- return con;
- }
-
-
- private static class ConnectionCloser implements Runnable
- {
- private static final Logger _log = Logger.getLogger(ConnectionCloser.class.getName());
-
-
- private final Connection c;
-
-
- private final RuntimeException exp;
-
-
- public ConnectionCloser(Connection con, RuntimeException e)
- {
- c = con;
- exp = e;
- }
-
- @Override
- public void run()
- {
- try
- {
- if (!c.isClosed())
- {
- _log.log(Level.WARNING, "Unclosed connection! Trace: " + exp.getStackTrace()[1], exp);
- }
- }
- catch (SQLException e)
- {
- _log.log(Level.WARNING, "", e);
- }
- }
- }
-
-
- private static ScheduledExecutorService getExecutor()
- {
- if (_executor == null)
- {
- synchronized (L2DatabaseFactory.class)
- {
- if (_executor == null)
- {
- _executor = Executors.newSingleThreadScheduledExecutor();
- }
- }
- }
- return _executor;
- }
-
-
- public int getBusyConnectionCount() throws SQLException
- {
- return _source.getNumBusyConnectionsDefaultUser();
- }
-
-
- public int getIdleConnectionCount() throws SQLException
- {
- return _source.getNumIdleConnectionsDefaultUser();
- }
- }
|