0
我正在使用多個線程將數百萬條記錄插入到數據庫中。 我正在使用3個課程。如何在多線程中使用連接池?
的getConnection:
import java.sql.Connection;
import java.sql.SQLException;
import org.apache.commons.dbcp2.BasicDataSource;
import LoadConfig;
public class DBCP {
private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger("DBCP");
private Connection conn;
public static Connection getOracleConnection() throws SQLException,
ClassNotFoundException {
String hostName = LoadConfig.Getconfig("hostName");
String sid = LoadConfig.Getconfig("sid");
String userName = LoadConfig.Getconfig("userName");
String password = LoadConfig.Getconfig("password");
return getOracleConnection(hostName, sid, userName, password);
}
private static Connection getOracleConnection(String hostName, String sid, String userName, String password)
throws SQLException {
BasicDataSource dataSource = new BasicDataSource();
try
{
dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
dataSource.setUrl("jdbc:oracle:thin:@" + hostName + ":1521:" + sid);
dataSource.setUsername(userName);
dataSource.setPassword(password);
dataSource.setMaxTotal(300);
return dataSource.getConnection();
} catch (SQLException ex) {
log.error("SQLException"+ex);
return null;
}
}
}
閱讀的文本:
public class Readtxt implements Runnable{
private static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger("ReadFile");
File file = new File(path);
ExecutorService executor = Executors.newFixedThreadPool(100);
@Override
public void run() {
try {
Connection conn = data.getOracleConnection()
try (BufferedReader br = new BufferedReader(new FileReader(file.getAbsolutePath()))) {
String line;
int i=0;
log.info("Start read and update file: " + fileName);
while ((line = br.readLine()) != null) {
Runnable Insert = new InsertDT(conn, line);
executor.execute(Insert);
}
log.info("Finish reading: " + fileName);
} catch (IOException ex) {
log.fatal("ReadFile IOException: " + ex);
} catch (Exception ex) {
log.fatal("Connect Error: " + ex);
}
}
}
和插入類:
public class InsertDT implements Runnable{
private static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger("InsertDT");
private String line
Connection conn = null;
CallableStatement statement;
public InsertDT(Connection _conn, String _line){
this.conn = _conn;
this.line = _line;
}
@Override
public void run() {
try {
try {
this.statement = conn.prepareCall("{ Call Produce (?,?)}");
statement.setString("Data", line);
statement.executeUpdate();
} catch (Exception ex) {
}
} catch (SQLException | ClassNotFoundException ex) {
log.error(ex);
}
}
}
但是親ject需要很多時間才能將記錄插入數據庫。有沒有人有任何想法如何解決這個問題?
嘗試批量插入而不是一次插入一個 – sidgate