Refactored DatabaseFactory.

This commit is contained in:
MobiusDev
2016-01-11 21:31:45 +00:00
parent d288d505a9
commit 2684901232
140 changed files with 668 additions and 955 deletions

View File

@ -14,37 +14,32 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jmobius.commons.database.pool.impl;
package com.l2jmobius.commons.database;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Logger;
import javax.sql.DataSource;
import com.l2jmobius.Config;
import com.l2jmobius.commons.database.pool.AbstractConnectionFactory;
import com.l2jmobius.commons.database.pool.IConnectionFactory;
import com.mchange.v2.c3p0.ComboPooledDataSource;
/**
* C3P0 Connection Factory implementation.<br>
* <b>Note that this class is not public to prevent external initialization.</b><br>
* <b>Access it through {@link ConnectionFactory} and proper configuration.</b>
* Database Factory implementation.
* @author Zoey76
*/
final class C3P0ConnectionFactory extends AbstractConnectionFactory
public class DatabaseFactory
{
private static final Logger LOG = Logger.getLogger(C3P0ConnectionFactory.class.getName());
private static final Logger _log = Logger.getLogger(DatabaseFactory.class.getName());
private final ComboPooledDataSource _dataSource;
public C3P0ConnectionFactory()
public DatabaseFactory()
{
if (Config.DATABASE_MAX_CONNECTIONS < 2)
{
Config.DATABASE_MAX_CONNECTIONS = 2;
LOG.warning("A minimum of " + Config.DATABASE_MAX_CONNECTIONS + " db connections are required.");
_log.warning("A minimum of 2 connections are required.");
}
_dataSource = new ComboPooledDataSource();
@ -57,10 +52,8 @@ final class C3P0ConnectionFactory extends AbstractConnectionFactory
_dataSource.setAcquireRetryAttempts(0); // try to obtain connections indefinitely (0 = never quit)
_dataSource.setAcquireRetryDelay(500); // 500 milliseconds wait before try to acquire connection again
_dataSource.setCheckoutTimeout(0); // 0 = wait indefinitely for new connection if pool is exhausted
_dataSource.setAcquireIncrement(5); // if pool is exhausted, get 5 more connections at a time
// cause there is a "long" delay on acquire connection
// so taking more than one connection at once will make connection pooling
// more effective.
_dataSource.setAcquireIncrement(5); // if pool is exhausted, get 5 more connections at a time cause there is
// a "long" delay on acquire connection so taking more than one connection at once will make connection pooling more effective.
// this "connection_test_table" is automatically created if not already there
_dataSource.setAutomaticTestTable("connection_test_table");
@ -70,19 +63,14 @@ final class C3P0ConnectionFactory extends AbstractConnectionFactory
_dataSource.setIdleConnectionTestPeriod(3600); // test idle connection every 60 sec
_dataSource.setMaxIdleTime(Config.DATABASE_MAX_IDLE_TIME); // 0 = idle connections never expire
// *THANKS* to connection testing configured above
// but I prefer to disconnect all connections not used
// for more than 1 hour
// *THANKS* to connection testing configured above but I prefer to disconnect all connections not used for more than 1 hour
// enables statement caching, there is a "semi-bug" in c3p0 0.9.0 but in 0.9.0.2 and later it's fixed
_dataSource.setMaxStatementsPerConnection(100);
_dataSource.setBreakAfterAcquireFailure(false); // never fail if any way possible
// setting this to true will make
// c3p0 "crash" and refuse to work
// till restart thus making acquire
// errors "FATAL" ... we don't want that
// it should be possible to recover
_dataSource.setBreakAfterAcquireFailure(false); // never fail if any way possible setting this to true will make c3p0 "crash"
// and refuse to work till restart thus making acquire errors "FATAL" ... we don't want that it should be possible to recover
try
{
_dataSource.setDriverClass(Config.DATABASE_DRIVER);
@ -104,14 +92,25 @@ final class C3P0ConnectionFactory extends AbstractConnectionFactory
{
e.printStackTrace();
}
if (Config.DEBUG)
{
LOG.fine("Database Connection Working");
}
}
@Override
public Connection getConnection()
{
Connection con = null;
while (con == null)
{
try
{
con = _dataSource.getConnection();
}
catch (SQLException e)
{
_log.warning(getClass().getSimpleName() + ": Unable to get a connection: " + e.getMessage());
}
}
return con;
}
public void close()
{
try
@ -120,23 +119,17 @@ final class C3P0ConnectionFactory extends AbstractConnectionFactory
}
catch (Exception e)
{
LOG.info(e.getMessage());
_log.info(e.getMessage());
}
}
@Override
public DataSource getDataSource()
{
return _dataSource;
}
public static IConnectionFactory getInstance()
public static DatabaseFactory getInstance()
{
return SingletonHolder.INSTANCE;
}
private static class SingletonHolder
{
protected static final IConnectionFactory INSTANCE = new C3P0ConnectionFactory();
protected static final DatabaseFactory INSTANCE = new DatabaseFactory();
}
}

View File

@ -1,48 +0,0 @@
/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jmobius.commons.database.pool;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Logger;
/**
* Abstract Connection Factory.
* @author Zoey76
*/
public abstract class AbstractConnectionFactory implements IConnectionFactory
{
private static final Logger LOG = Logger.getLogger(AbstractConnectionFactory.class.getName());
@Override
public Connection getConnection()
{
Connection con = null;
while (con == null)
{
try
{
con = getDataSource().getConnection();
}
catch (SQLException e)
{
LOG.warning(getClass().getSimpleName() + ": Unable to get a connection: " + e.getMessage());
}
}
return con;
}
}

View File

@ -1,46 +0,0 @@
/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jmobius.commons.database.pool;
import java.sql.Connection;
import javax.sql.DataSource;
/**
* Connection Factory interface.
* @author Zoey76
*/
public interface IConnectionFactory
{
/**
* Gets the data source.
* @return the data source
*/
DataSource getDataSource();
/**
* Gets a connection from the pool.
* @return a connection
*/
Connection getConnection();
/**
* Closes the data source.<br>
* <i>Same as shutdown.</i>
*/
void close();
}

View File

@ -1,64 +0,0 @@
/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jmobius.commons.database.pool.impl;
import java.util.logging.Logger;
import javax.sql.DataSource;
import com.l2jmobius.commons.database.pool.AbstractConnectionFactory;
import com.l2jmobius.commons.database.pool.IConnectionFactory;
/**
* BoneCP Connection Factory implementation.<br>
* <b>Note that this class is not public to prevent external initialization.</b><br>
* <b>Access it through {@link ConnectionFactory} and proper configuration.</b>
* @author Zoey76
*/
final class BoneCPConnectionFactory extends AbstractConnectionFactory
{
private static final Logger LOG = Logger.getLogger(BoneCPConnectionFactory.class.getName());
private final DataSource _dataSource = null;
public BoneCPConnectionFactory()
{
LOG.severe("BoneCP is not supported yet, nothing is going to work!");
}
@Override
public void close()
{
throw new UnsupportedOperationException("BoneCP is not supported yet!");
}
@Override
public DataSource getDataSource()
{
return _dataSource;
}
public static IConnectionFactory getInstance()
{
return SingletonHolder.INSTANCE;
}
private static class SingletonHolder
{
protected static final IConnectionFactory INSTANCE = new BoneCPConnectionFactory();
}
}

View File

@ -1,59 +0,0 @@
/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jmobius.commons.database.pool.impl;
import com.l2jmobius.Config;
import com.l2jmobius.commons.database.pool.IConnectionFactory;
/**
* Connection Factory implementation.
* @author Zoey76
*/
public class ConnectionFactory
{
public static IConnectionFactory getInstance()
{
return SingletonHolder.INSTANCE;
}
private static class SingletonHolder
{
protected static final IConnectionFactory INSTANCE;
static
{
switch (Config.DATABASE_CONNECTION_POOL)
{
default:
case "C3P0":
{
INSTANCE = new C3P0ConnectionFactory();
break;
}
case "HikariCP":
{
INSTANCE = new HikariCPConnectionFactory();
break;
}
case "BoneCP":
{
INSTANCE = new BoneCPConnectionFactory();
break;
}
}
}
}
}

View File

@ -1,64 +0,0 @@
/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jmobius.commons.database.pool.impl;
import java.util.logging.Logger;
import javax.sql.DataSource;
import com.l2jmobius.commons.database.pool.AbstractConnectionFactory;
import com.l2jmobius.commons.database.pool.IConnectionFactory;
/**
* HikariCP Connection Factory implementation.<br>
* <b>Note that this class is not public to prevent external initialization.</b><br>
* <b>Access it through {@link ConnectionFactory} and proper configuration.</b>
* @author Zoey76
*/
final class HikariCPConnectionFactory extends AbstractConnectionFactory
{
private static final Logger LOG = Logger.getLogger(HikariCPConnectionFactory.class.getName());
private final DataSource _dataSource = null;
public HikariCPConnectionFactory()
{
LOG.severe("HikariCP is not supported yet, nothing is going to work!");
}
@Override
public void close()
{
throw new UnsupportedOperationException("HikariCP is not supported yet!");
}
@Override
public DataSource getDataSource()
{
return _dataSource;
}
public static IConnectionFactory getInstance()
{
return SingletonHolder.INSTANCE;
}
private static class SingletonHolder
{
protected static final IConnectionFactory INSTANCE = new HikariCPConnectionFactory();
}
}