在多线程中,将Spring jdbctemplate与Spring 3.0结合使用时,
无法获得JDBC连接
或
没有可用的缓冲区空间错误
我创建了一个应用程序,在其中使用多线程对大约10万条记录进行操作。我已经使用spring jdbctemplate进行数据库操作。我正在创建50个线程来处理100k条记录,因此一个线程将不得不处理20k条记录。对于每个记录,将完成一些操作,并将其存储在多个表中。
for (int j = 0; j < nMaxThreads; j++) {
Thread thTread = new Thread() {
@Override
public void run() {
logger.error(this.getName() + " Started");
try {
-- Process and do database operations --
} catch (Exception e) {
logger.error("Exception occured while thread: " + this.getName(), e);
}
logger.error(this.getName() + " Finished");
}
};
thTread.setName("JobCalcThread-" + j);
thTread.start();
处理完某些记录后,我随机遇到错误,并且所有线程都停止处理。
2019-04-10 19:50:17 ERROR TaxCalculateHelper:387 - Exception Occured:
org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Last packet sent to the server was 0 ms ago.
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:575)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:818)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:874)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:882)
at com.pnp.dao.TaxCalcDao.saveUserCurrentTaxInfo(TaxCalcDao.java:670)
at com.pnp.taxcalculator.TaxCalculateHelper.startTaxCalcProcessForUsers(TaxCalculateHelper.java:378)
at com.pnp.taxcalculator.TaxCalculateHelper$1.run(TaxCalculateHelper.java:351)
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
或
Caused by: java.net.SocketException: No buffer space available (maximum connections reached?): connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:276)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2027)
... 23 more
注意:所有线程都在同一表上执行操作