|
@@ -26,15 +26,19 @@ import com.l2jserver.gameserver.ThreadPoolManager;
|
|
import com.mchange.v2.c3p0.ComboPooledDataSource;
|
|
import com.mchange.v2.c3p0.ComboPooledDataSource;
|
|
|
|
|
|
/**
|
|
/**
|
|
- * This class manages the database connections.<br>
|
|
|
|
|
|
+ * This class manages the database connections.
|
|
*/
|
|
*/
|
|
public class L2DatabaseFactory
|
|
public class L2DatabaseFactory
|
|
{
|
|
{
|
|
private static final Logger _log = Logger.getLogger(L2DatabaseFactory.class.getName());
|
|
private static final Logger _log = Logger.getLogger(L2DatabaseFactory.class.getName());
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * The Enum ProviderType.
|
|
|
|
+ */
|
|
public static enum ProviderType
|
|
public static enum ProviderType
|
|
{
|
|
{
|
|
- MySql, MsSql
|
|
|
|
|
|
+ MySql,
|
|
|
|
+ MsSql
|
|
}
|
|
}
|
|
|
|
|
|
private static L2DatabaseFactory _instance;
|
|
private static L2DatabaseFactory _instance;
|
|
@@ -42,6 +46,10 @@ public class L2DatabaseFactory
|
|
private ProviderType _providerType;
|
|
private ProviderType _providerType;
|
|
private ComboPooledDataSource _source;
|
|
private ComboPooledDataSource _source;
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Instantiates a new l2 database factory.
|
|
|
|
+ * @throws SQLException the SQL exception
|
|
|
|
+ */
|
|
public L2DatabaseFactory() throws SQLException
|
|
public L2DatabaseFactory() throws SQLException
|
|
{
|
|
{
|
|
try
|
|
try
|
|
@@ -98,28 +106,46 @@ public class L2DatabaseFactory
|
|
_source.getConnection().close();
|
|
_source.getConnection().close();
|
|
|
|
|
|
if (Config.DEBUG)
|
|
if (Config.DEBUG)
|
|
|
|
+ {
|
|
_log.fine("Database Connection Working");
|
|
_log.fine("Database Connection Working");
|
|
|
|
+ }
|
|
|
|
|
|
if (Config.DATABASE_DRIVER.toLowerCase().contains("microsoft"))
|
|
if (Config.DATABASE_DRIVER.toLowerCase().contains("microsoft"))
|
|
|
|
+ {
|
|
_providerType = ProviderType.MsSql;
|
|
_providerType = ProviderType.MsSql;
|
|
|
|
+ }
|
|
else
|
|
else
|
|
|
|
+ {
|
|
_providerType = ProviderType.MySql;
|
|
_providerType = ProviderType.MySql;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
catch (SQLException x)
|
|
catch (SQLException x)
|
|
{
|
|
{
|
|
if (Config.DEBUG)
|
|
if (Config.DEBUG)
|
|
|
|
+ {
|
|
_log.fine("Database Connection FAILED");
|
|
_log.fine("Database Connection FAILED");
|
|
|
|
+ }
|
|
// re-throw the exception
|
|
// re-throw the exception
|
|
throw x;
|
|
throw x;
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
catch (Exception e)
|
|
{
|
|
{
|
|
if (Config.DEBUG)
|
|
if (Config.DEBUG)
|
|
|
|
+ {
|
|
_log.fine("Database Connection FAILED");
|
|
_log.fine("Database Connection FAILED");
|
|
|
|
+ }
|
|
throw new SQLException("Could not init DB connection:" + e.getMessage());
|
|
throw new SQLException("Could not init DB connection:" + e.getMessage());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Prepared query select.
|
|
|
|
+ * @param fields the fields
|
|
|
|
+ * @param tableName the table name
|
|
|
|
+ * @param whereClause the where clause
|
|
|
|
+ * @param returnOnlyTopRecord the return only top record
|
|
|
|
+ * @return the string
|
|
|
|
+ */
|
|
public final String prepQuerySelect(String[] fields, String tableName, String whereClause, boolean returnOnlyTopRecord)
|
|
public final String prepQuerySelect(String[] fields, String tableName, String whereClause, boolean returnOnlyTopRecord)
|
|
{
|
|
{
|
|
String msSqlTop1 = "";
|
|
String msSqlTop1 = "";
|
|
@@ -127,14 +153,21 @@ public class L2DatabaseFactory
|
|
if (returnOnlyTopRecord)
|
|
if (returnOnlyTopRecord)
|
|
{
|
|
{
|
|
if (getProviderType() == ProviderType.MsSql)
|
|
if (getProviderType() == ProviderType.MsSql)
|
|
|
|
+ {
|
|
msSqlTop1 = " Top 1 ";
|
|
msSqlTop1 = " Top 1 ";
|
|
|
|
+ }
|
|
if (getProviderType() == ProviderType.MySql)
|
|
if (getProviderType() == ProviderType.MySql)
|
|
|
|
+ {
|
|
mySqlTop1 = " Limit 1 ";
|
|
mySqlTop1 = " Limit 1 ";
|
|
|
|
+ }
|
|
}
|
|
}
|
|
String query = "SELECT " + msSqlTop1 + safetyString(fields) + " FROM " + tableName + " WHERE " + whereClause + mySqlTop1;
|
|
String query = "SELECT " + msSqlTop1 + safetyString(fields) + " FROM " + tableName + " WHERE " + whereClause + mySqlTop1;
|
|
return query;
|
|
return query;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Shutdown.
|
|
|
|
+ */
|
|
public void shutdown()
|
|
public void shutdown()
|
|
{
|
|
{
|
|
try
|
|
try
|
|
@@ -155,6 +188,11 @@ public class L2DatabaseFactory
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Safety string.
|
|
|
|
+ * @param whatToCheck the what to check
|
|
|
|
+ * @return the string
|
|
|
|
+ */
|
|
public final String safetyString(String... whatToCheck)
|
|
public final String safetyString(String... whatToCheck)
|
|
{
|
|
{
|
|
// NOTE: Use brace as a safety precaution just in case name is a reserved word
|
|
// NOTE: Use brace as a safety precaution just in case name is a reserved word
|
|
@@ -196,6 +234,11 @@ public class L2DatabaseFactory
|
|
return sbResult.toString();
|
|
return sbResult.toString();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Gets the single instance of L2DatabaseFactory.
|
|
|
|
+ * @return single instance of L2DatabaseFactory
|
|
|
|
+ * @throws SQLException the SQL exception
|
|
|
|
+ */
|
|
public static L2DatabaseFactory getInstance() throws SQLException
|
|
public static L2DatabaseFactory getInstance() throws SQLException
|
|
{
|
|
{
|
|
synchronized (L2DatabaseFactory.class)
|
|
synchronized (L2DatabaseFactory.class)
|
|
@@ -208,6 +251,10 @@ public class L2DatabaseFactory
|
|
return _instance;
|
|
return _instance;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Gets the connection.
|
|
|
|
+ * @return the connection
|
|
|
|
+ */
|
|
public Connection getConnection()
|
|
public Connection getConnection()
|
|
{
|
|
{
|
|
Connection con = null;
|
|
Connection con = null;
|
|
@@ -217,9 +264,13 @@ public class L2DatabaseFactory
|
|
{
|
|
{
|
|
con = _source.getConnection();
|
|
con = _source.getConnection();
|
|
if (Server.serverMode == Server.MODE_GAMESERVER)
|
|
if (Server.serverMode == Server.MODE_GAMESERVER)
|
|
|
|
+ {
|
|
ThreadPoolManager.getInstance().scheduleGeneral(new ConnectionCloser(con, new RuntimeException()), Config.CONNECTION_CLOSE_TIME);
|
|
ThreadPoolManager.getInstance().scheduleGeneral(new ConnectionCloser(con, new RuntimeException()), Config.CONNECTION_CLOSE_TIME);
|
|
|
|
+ }
|
|
else
|
|
else
|
|
|
|
+ {
|
|
getExecutor().schedule(new ConnectionCloser(con, new RuntimeException()), 60, TimeUnit.SECONDS);
|
|
getExecutor().schedule(new ConnectionCloser(con, new RuntimeException()), 60, TimeUnit.SECONDS);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
catch (SQLException e)
|
|
catch (SQLException e)
|
|
{
|
|
{
|
|
@@ -229,11 +280,22 @@ public class L2DatabaseFactory
|
|
return con;
|
|
return con;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * The Class ConnectionCloser.
|
|
|
|
+ */
|
|
private static class ConnectionCloser implements Runnable
|
|
private static class ConnectionCloser implements Runnable
|
|
{
|
|
{
|
|
|
|
+ /** The connection. */
|
|
private final Connection c;
|
|
private final Connection c;
|
|
|
|
+
|
|
|
|
+ /** The exception. */
|
|
private final RuntimeException exp;
|
|
private final RuntimeException exp;
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Instantiates a new connection closer.
|
|
|
|
+ * @param con the con
|
|
|
|
+ * @param e the e
|
|
|
|
+ */
|
|
public ConnectionCloser(Connection con, RuntimeException e)
|
|
public ConnectionCloser(Connection con, RuntimeException e)
|
|
{
|
|
{
|
|
c = con;
|
|
c = con;
|
|
@@ -257,10 +319,16 @@ public class L2DatabaseFactory
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Close the connection.
|
|
|
|
+ * @param con the con
|
|
|
|
+ */
|
|
public static void close(Connection con)
|
|
public static void close(Connection con)
|
|
{
|
|
{
|
|
if (con == null)
|
|
if (con == null)
|
|
|
|
+ {
|
|
return;
|
|
return;
|
|
|
|
+ }
|
|
|
|
|
|
try
|
|
try
|
|
{
|
|
{
|
|
@@ -272,6 +340,10 @@ public class L2DatabaseFactory
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Gets the executor.
|
|
|
|
+ * @return the executor
|
|
|
|
+ */
|
|
private static ScheduledExecutorService getExecutor()
|
|
private static ScheduledExecutorService getExecutor()
|
|
{
|
|
{
|
|
if (_executor == null)
|
|
if (_executor == null)
|
|
@@ -279,22 +351,38 @@ public class L2DatabaseFactory
|
|
synchronized (L2DatabaseFactory.class)
|
|
synchronized (L2DatabaseFactory.class)
|
|
{
|
|
{
|
|
if (_executor == null)
|
|
if (_executor == null)
|
|
|
|
+ {
|
|
_executor = Executors.newSingleThreadScheduledExecutor();
|
|
_executor = Executors.newSingleThreadScheduledExecutor();
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return _executor;
|
|
return _executor;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Gets the busy connection count.
|
|
|
|
+ * @return the busy connection count
|
|
|
|
+ * @throws SQLException the SQL exception
|
|
|
|
+ */
|
|
public int getBusyConnectionCount() throws SQLException
|
|
public int getBusyConnectionCount() throws SQLException
|
|
{
|
|
{
|
|
return _source.getNumBusyConnectionsDefaultUser();
|
|
return _source.getNumBusyConnectionsDefaultUser();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Gets the idle connection count.
|
|
|
|
+ * @return the idle connection count
|
|
|
|
+ * @throws SQLException the SQL exception
|
|
|
|
+ */
|
|
public int getIdleConnectionCount() throws SQLException
|
|
public int getIdleConnectionCount() throws SQLException
|
|
{
|
|
{
|
|
return _source.getNumIdleConnectionsDefaultUser();
|
|
return _source.getNumIdleConnectionsDefaultUser();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Gets the provider type.
|
|
|
|
+ * @return the provider type
|
|
|
|
+ */
|
|
public final ProviderType getProviderType()
|
|
public final ProviderType getProviderType()
|
|
{
|
|
{
|
|
return _providerType;
|
|
return _providerType;
|