使用Apache公共DBCP和DBUtils以及并发连接池

时间:2013-06-13 08:58:25

标签: concurrency connection-pooling apache-commons-dbcp apache-commons-dbutils jdbc-pool

我正在研究在不同线程中进行多个查询时的并发性。 我使用Apache DBCP和DBUtils不是因为我想让我的生活复杂化,而是因为他们应该保证查询得到正确处理,因此并发。

然而,即使使用上述酷工具,我也得到了:

Error : org.h2.jdbc.JdbcSQLException: Das Objekt wurde bereits geschlossen
The object is already closed [90007-148]
Error : java.lang.NullPointerException

当手动使用Database和Connection对象时,我也遇到了同样的错误。 它每运行5-6次就会发生一次,但这只是一个玩具程序,在现实世界的应用程序中,这种错误会不断出现。

在我的示例代码下面

DatatTransaction.java

import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;
import org.apache.commons.dbcp.BasicDataSource;

public class DataTransaction
{
    private final static String username = "";
    private final static String password = "";
    private final static String url = "db" + File.separator + "persondb;create=true";
    public static Connection connection = null;
    public static BasicDataSource dataSource;

    public DataTransaction(boolean setCon)
    {
        try
        {
            setConnectionTest();
        }
        catch (Exception e)
        {
            System.out.println("Error in Connection:" + e.toString());
        }
    }

    public final void setConnectionTest() throws SQLException
    {
        try
        {
            if (dataSource == null)
            {
                dataSource = new BasicDataSource();
                String driver = "org.h2.Driver";
                try
                {
                    dataSource.setDriverClassName(driver);
                    dataSource.setUrl("jdbc:h2:"+url);
                    dataSource.setUsername(username);
                    dataSource.setPassword(password);
                    dataSource.setMaxActive(100);
                    dataSource.setMaxWait(10000);
                    dataSource.setMaxIdle(10);
                    if (connection == null || connection.isClosed())
                    {
                        connection = dataSource.getConnection();
                    }
                }
                catch (SQLException e)
                {
                    System.out.println("Could not connect to the database msg :" + e.getMessage());
                }
            }
            else
            {
                connection = dataSource.getConnection();
            }
        }
        catch (Exception e)
        {
            System.out.println("open connection exception" + e);
        }
    }
}

DBTest2.java

package dbtest;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class DBTest2
{

    public static void main(String[] args)
    {
        try
        {
            new Thread(db1).start();
            new Thread(db2).start();
        }
        catch (Exception e)
        {
            System.out.println("MM : Error : " + e);
        }
    }
    private static Runnable db1 = new Runnable()
    {
        public void run()
        {
            try
            {
                for (int i = 0; i < 50; i++)
                {
                    DBTest2 dBTest = new DBTest2();
                    List<Object[]> list1 = dBTest.DB1();
                    for (Object[] object : list1)
                    {
                        System.out.println("DB1 : FirstName : " + object[0] + " Lastname: " + object[1]);
                    }
                }
            }
            catch (Exception e)
            {
                System.out.println("Error : " + e);
            }
        }
    };

    private static Runnable db2 = new Runnable()
    {
        public void run()
        {
            try
            {
                for (int i = 0; i < 50; i++)
                {
                    DBTest2 dBTest = new DBTest2();
                    List<Object[]> list = dBTest.DB2();
                    for (Object[] object : list)
                    {
                        System.out.println("DB2 : FirstName : " + object[0] + " Lastname: " + object[1]);
                    }
                }
            }
            catch (Exception e)
            {
                System.out.println("Error : " + e);
            }
        }
    };

    public List<Object[]> DB1()
    {
        try
        {
            DataTransaction dt = new DataTransaction(true);
            Connection conn = dt.connection;
            Statement statement = conn.createStatement();
            ResultSet rs = statement.executeQuery("select NAME,SURNAME from PERSON");
            ResultSetMetaData rsmd = rs.getMetaData();
            int dataCnt = rsmd.getColumnCount();
            List<Object[]> list = new ArrayList<Object[]>();
            while (rs.next())
            {
                Object[] data = new Object[dataCnt];
                for (int i = 0; i < dataCnt; i++)
                {
                    data[i] = rs.getString(i + 1);
                }
                list.add(data);
            }
            conn.close();
            return list;
        }
        catch (Exception e)
        {
            System.out.println("Error : " + e);
            return null;
        }
    }

    public List<Object[]> DB2()
    {
        try
        {
            DataTransaction dt = new DataTransaction(true);
            Connection conn = dt.connection;
            Statement statement = conn.createStatement();
            ResultSet rs = statement.executeQuery("select NAME,SURNAME from PERSON");
            ResultSetMetaData rsmd = rs.getMetaData();
            int dataCnt = rsmd.getColumnCount();
            List<Object[]> list = new ArrayList<Object[]>();
            while (rs.next())
            {
                Object[] data = new Object[dataCnt];
                for (int i = 0; i < dataCnt; i++)
                {
                    data[i] = rs.getString(i + 1);
                }
                list.add(data);
            }
            conn.close();
            return list;
        }
        catch (Exception e)
        {
            System.out.println("Error : " + e);
            return null;
        }
    }
}

1 个答案:

答案 0 :(得分:4)

你应该读一下derby pitfalls里面有一些常见的陷阱,你感兴趣的是:

“执行语句会自动关闭由该语句的早期执行生成的任何现有打开的ResultSet。如果线程共享语句,则一个线程可以关闭另一个的ResultSet。在许多情况下,更容易将每个线程分配给不同的Connection。 “

好的,你的代码问题是DatatTransaction.java,通过删除静态变量连接更改代码并添加此方法:

    public final Connection getConnection()
{
    Connection conn = null;
    try
    {
        conn = dataSource.getConnection();
    }
    catch (SQLException e)
    {
        System.out.println("Could not connect to the database msg :" + e.getMessage());
    }
    return conn;
}

现在它将不再提出任何问题(顺便说一下,如果你在你的例子中注释System.out.println(),你会更快速地看到并发错误,否则这些错误会被所需的时间减少打印到控制台。)

至于“exceptionorg.apache.commons.dbcp.SQLNestedException:无法获取连接,池错误超时等待空闲对象”,这是因为你没有通过注释close()方法正确关闭资源,所以你很快用完了游泳池中的连接。