我正在研究在不同线程中进行多个查询时的并发性。我使用 Apache DBCP 和 DBUtils 不是因为我想让我的生活复杂化,而是因为它们应该保证查询得到正确处理以及并发性。
但是,即使使用上述很酷的工具,我也会得到:
Error : org.h2.jdbc.JdbcSQLException: Das Objekt wurde bereits geschlossen
The object is already closed [90007-148]
Error : java.lang.NullPointerException
这与我在手动使用数据库和连接对象时遇到的错误相同。程序每运行 5-6 次就会发生一次,但这只是一个玩具程序,在现实世界的应用程序中,这种错误会不断弹出。
在我的示例代码下面
数据交易.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;
}
}
}