我非常熟悉在tomcat上使用连接池&我已经使用它很多年了,没有任何问题。然而,目前我正在开发一个main方法应用程序,由于性能原因,该应用程序需要同时运行多个线程,而这些线程每个都需要访问同一个数据库。如果我完全剥离数据库代码&只是出于测试目的使用数组(例如,多线程工作),那么我的代码就可以工作了。然而,一旦我添加回数据库连接,第一个线程就获得了锁,而其他线程根本不会运行。使用过c3p0和dbcp2;目前正在使用dbcp2。谢谢!有大量的文档,但似乎没有太多特定于我的用例的代码示例。下面是一个示例应用程序:
import java.sql.*;
import org.apache.commons.dbcp2.ConnectionFactory;
import org.apache.commons.dbcp2.DriverManagerConnectionFactory;
import org.apache.commons.dbcp2.PoolableConnection;
import org.apache.commons.dbcp2.PoolableConnectionFactory;
import org.apache.commons.dbcp2.PoolingDataSource;
import org.apache.commons.dbcp2.PoolingDriver;
import org.apache.commons.dbcp2.Utils;
import org.apache.commons.pool2.ObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPool;
public class SandboxApp {
private static BasicDataSource dataSource;
public static BasicDataSource getDataSource() {
if (dataSource == null) {
BasicDataSource ds = new BasicDataSource();
ds.setUrl("jdbc:mysql://localhost:3306/my-db");
ds.setUsername("root");
ds.setPassword("");
ds.setDriverClassName("org.mariadb.jdbc.Driver");
ds.setInitialSize(3);
ds.setMaxTotal(25);
ds.setMinIdle(0);
ds.setMaxIdle(8);
ds.setMaxOpenPreparedStatements(100);
dataSource = ds;
}
return dataSource;
}
public static void main(String [] args) throws Exception{
for(int i=0; i<11; i++){//spawn 11 threads & get each thread to process 600k sql rows at the same time
new Thread("" + (i*600000)){
public void run(){
System.out.println("Thread: " + getName() + " running");//prints correctly for all threads
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = SandboxApp.getDataSource().getConnection();
pstmt = con.prepareStatement("select something from some_table limit "+getName()+",600000");
rs=pstmt.executeQuery();
while(rs.next()){
System.out.println("Doing stuff for thread "+getName());//this only prints for getName() == 0
//give the other threads a turn...
try {
Thread.sleep(10);
}
catch(InterruptedException ex) {
}
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {pstmt.close();} catch (SQLException e) {}
try { con.close(); } catch(Exception e) {}
try { rs.close(); } catch(Exception e) {}
}
}
}.start();
}
}
}发布于 2020-06-02 01:08:24
@user207421是对的,getDataSource()方法应该是同步的&当然我已经尝试过了;但是这仍然不能解决我的线程"0“不让其他线程轮流的问题。
我从我的代码中剥离了所有东西,所有其他的库等等。直到我让它工作,然后又开始重新构建它,以找到断裂点。似乎主要的决定因素是ResultSet的大小。我尝试在不同的地方添加额外的thread.sleep时间,但是唯一起作用的是分解查询以请求更小的ResultSets。
600k结果集,只有1个线程运行,1k ResultSets和4个线程运行。在ResultSets只包含100行的情况下,所有11个线程都将运行。请注意,我是在一个16 CPU的系统上进行测试的,该系统分配了8 8GB的内存给JVM (aws m5.4xlarge),所以硬件资源不应该是一个因素。所以我想我只能把我的代码分成更小的块。
当我最初研究这个问题时,我很惊讶地发现这个问题缺乏特定的代码样本(不管ResultSet的大小和线程的数量),所以我只是为了完整的代码样本而在这里发布最终对我有效的代码样本:
import java.sql.*;
import org.apache.commons.dbcp2.BasicDataSource;
public class SandboxApp {
private static BasicDataSource dataSource;
public static synchronized BasicDataSource getDataSource() {
if (dataSource == null) {
BasicDataSource ds = new BasicDataSource();
ds.setUrl("jdbc:mysql://localhost:3306/my-db");
ds.setUsername("root");
ds.setPassword("");
ds.setDriverClassName("org.mariadb.jdbc.Driver");
ds.setInitialSize(3);
ds.setMaxTotal(25);
ds.setMinIdle(0);
ds.setMaxIdle(8);
ds.setMaxOpenPreparedStatements(100);
dataSource = ds;
}
return dataSource;
}
public static void main(String [] args) throws Exception{
for(int i=0; i<11; i++){//spawn 11 threads & get each thread to process 100 sql rows at the same time
new Thread("" + (i*100)){
public void run(){
System.out.println("Thread: " + getName() + " running");
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = SandboxApp.getDataSource().getConnection();
pstmt = con.prepareStatement("select something from some_table limit "+getName()+",100");
rs=pstmt.executeQuery();
while(rs.next()){
System.out.println("Doing stuff for thread "+getName());//With smaller ResultSet, this works fine for all 11 threads
//give the other threads a turn...
try {
Thread.sleep(10);
}
catch(InterruptedException ex) {
}
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {pstmt.close();} catch (SQLException e) {}
try { con.close(); } catch(Exception e) {}
try { rs.close(); } catch(Exception e) {}
}
}
}.start();
}
}
}https://stackoverflow.com/questions/62094801
复制相似问题