javaweb服务MYSQL连接管理

javaweb服务MYSQL连接管理,java,web-services,jdbc,jdbc-pool,Java,Web Services,Jdbc,Jdbc Pool,我被web服务中的MySQL连接卡住了 public class DBAccessLayer { private Connection connection = null; private Statement statement = null; private String DBFormatter = "'"; private String key; private static DataSource datasource = null; priv

我被web服务中的MySQL连接卡住了

public class DBAccessLayer
{
    private Connection connection = null;
    private Statement statement = null;
    private String DBFormatter = "'";
    private String key;
    private static DataSource datasource = null;
    private static boolean _isInitiated = false;
    private static boolean _isInitializing = false;

    public DBAccessLayer()
    {
        key = ConstantValues.getKey();
        SetPoolSettings();
    }

    private void SetPoolSettings()
    {
        try
        {
            if (!_isInitiated && !_isInitializing)
            {
                _isInitializing = true;

                PoolProperties p = new PoolProperties();
                p.setUrl("jdbc:mysql://localhost:3306/DBName?autoReconnect=true");
                p.setDriverClassName("com.mysql.jdbc.Driver");
                p.setUsername("root");
                p.setPassword("password");
                p.setJmxEnabled(true);
                p.setTestWhileIdle(false);
                p.setTestOnBorrow(true);
                p.setValidationQuery("SELECT 1");
                p.setTestOnReturn(false);
                p.setValidationInterval(30000);
                p.setTimeBetweenEvictionRunsMillis(30000);
                p.setMaxActive(100);
                p.setInitialSize(10);
                p.setMaxWait(10000);
                p.setRemoveAbandonedTimeout(60);
                p.setMinEvictableIdleTimeMillis(30000);
                p.setMinIdle(10);
                p.setLogAbandoned(true);
                p.setRemoveAbandoned(true);
                p.setFairQueue(true);
                p
                    .setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
                datasource = new DataSource();
                datasource.setPoolProperties(p);

                _isInitiated = true;
            }
        }
        finally
        {
            _isInitializing = false;
        }
    }

    public void OpenConnection() throws SQLException
    {
        if (connection == null || connection.isClosed())
            connection = datasource.getConnection();

        if (statement == null || statement.isClosed())
            statement = connection.createStatement();

    }

    public Statement getStatement() throws Exception
    {
        if (connection == null || connection.isClosed())
            connection = datasource.getConnection();
        statement = connection.createStatement();
        return statement;
    }

    public void CloseConnection()
    {
        try
        {
            if (statement != null)
                statement.close();
        }
        catch (Exception ignore)
        {
            ignore.printStackTrace();
        }

        try
        {
            if (connection != null)
                connection.close();
        }
        catch (Exception ignore)
        {
            ignore.printStackTrace();
        }
    }
}
这是我用来连接web服务中数据库的类。。我有一些quartz计划的作业使用相同的web服务运行

我在每次web服务方法调用和每次作业启动时以及在finally block CloseConnection()中调用OpenConnection()方法

但我经常会遇到这个错误,而我在try-finally块中只关闭了一次连接

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:语句关闭后不允许执行任何操作

所以我的web服务方法看起来像

private DBAccessLayer _DBAccessLayer = null;

private DBAccessLayer getDBAccessLayer()
{
    if (_DBAccessLayer == null)
        _DBAccessLayer = new DBAccessLayer();
    rerurn _DBAccessLayer;
}

public void dummyCall()
{
    try
    {

        getDBAccessLayer.getStatement().executeQuery("Some query");

    }
    finally
    {
        getDBAccessLayer.CloseConnection();
    }
}

运行时中存在多少个
DBAccessLayer
实例?希望每个电话一个。。。请记住,给定的连接不能在多个线程中并行使用。不使用标准连接池的具体原因是什么?此代码似乎不是线程安全的,正在web服务中使用。为每个调用创建一个新的“DBAccessLayer”对象。我删除了OpenConnection调用,并让它由TomcatApacheJDBC池处理@Vikdor这就是为什么我使用jdbc池,而我的web服务正在tomcat服务器上运行。又出现了一个错误:-com.mysql.jdbc.ResultSetImpl.next(ResultSetImpl.java:7008)上的java.lang.NullPointerException,错误消息为nullMore。情况更窄了,这两个错误来自一个方法,该方法是每个web服务方法调用的身份验证模块。其中,我们在两个查询中验证令牌和更新日期时间。