天天看點

java類大批量導入資料到MYSQL

      今天在寫一個java類,用jdbc批量更新某表一個字段,該字段需要查詢其他多張表進行計算得出,中間用了許多select查詢,最後拼裝成update語句用jdbc操作,當導入到200多條的時候,報錯了,提示資料庫連接配接已經用完(我每次調用的時候連接配接都記得關閉了,程式邏輯沒有問題),具體資訊如下:

15:50:12 [ERROR] com.zyn.hibernate.test.DataSourceHelper.getConnectionByJdbc(76)
	使用JDBC資料源獲得資料庫連接配接出錯:The driver was unable to create a connection due to 

an inability to establish the client portion of a socket.

This is usually caused by a limit on the number of sockets imposed by the operating system. 

This limit is usually configurable. 

For Unix-based platforms, see the manual page for the 'ulimit' command. Kernel or system 

reconfiguration may also be required.

For Windows-based platforms, see Microsoft Knowledge Base Article 196271 (Q196271).

-- listing properties --
jdbc.url=jdbc:mysql://192.168.1.211:3306/chart...
jdbc.username=root
jdbc.password=123
           
jdbc.driverClassName=com.mysql.jdbc.Driver
chinahrt.url=jdbc:mysql://192.168.1.211:3306/china...
15:50:12 [ERROR] com.zyn.hibernate.test.JdbcManager.getObjectListBySQL(391)
	無法獲得資料庫連接配接!

java.lang.RuntimeException: 無法獲得資料庫連接配接!
	at com.zyn.hibernate.test.DataSourceHelperH
           

 網上搜尋後得知,可以調大資料庫的最大連接配接數來解決該問題,但問題是我沒有資料庫所在linux伺服器的賬号和密碼,用jdbc操作,也不可能使用hibernate的連接配接池來解決該問題,自己再寫個連接配接池貌似也劃不來,隻能采用比較耗時的操作,在進行沒次update操作的代碼中間加了個 

try{
				Thread.sleep(500);
			}catch(Exception e){
				e.printStackTrace();
			}
           

來解決問題。幸好資料量不是太多,這點時間還忍受的了。