java mysql 线程安全_java连接mysql的线程安全问题
稍微修改了下,可能會好一些,建議還是聽上面那哥們的,使用成熟的數據庫連接池,沒必要重復造輪子
使用單例,保證數據庫連接的唯一性
修改synchronized關鍵字的用法,提高效率
增加volatile關鍵字,提高穩定性
package com.singleton;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 功能:
*
* 完整路徑: com.singleton.MySQLUtil
* 創建日期: 2017年6月15日 上午10:42:49
*
* @author pfyangf
* @version 1.0
*/
class MySQLUtil {
private MySQLUtil(){}
private static volatile Connection connection = null;
private static final String driver = "com.mysql.jdbc.Driver";
private static final String url = "jdbc:mysql://192.168.31.103:3306/";
private static final String character = "?useUnicode=true&characterEncoding=utf8";
private static final String ssl = "&useSSL=false";
private static final String user = "axtest";
private static final String password = "axtest123";
private static Statement statement = null;
private static PreparedStatement ps = null;
private static ResultSet rs = null;
public static void main(String[] args) {
/*Connection newConnection;
try {
newConnection = MySQLUtil.connectToDB("xxx");
System.out.println(newConnection.isClosed());
} catch (Exception e) {
//TODO 異常處理
e.printStackTrace();
}*/
try {
List> data = MySQLUtil.readData("xxx", "select now() from dual");
System.out.println(data.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
boolean TestConnection(String db) {
try {
Class.forName(driver);
Connection connection = DriverManager.getConnection(url + db + character + ssl, user, password);
if (!connection.isClosed()) {
CloseConnection();
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 功能:獲取DB連接
*
* @Author:pfyangf , 2017年6月15日
* @param db
* @return
* @throws Exception Connection
**/
public static Connection connectToDB(String db) throws Exception {
if(null == connection){
synchronized (MySQLUtil.class) {
if(null == connection){
Class.forName(driver);
connection = DriverManager.getConnection(url + db + character + ssl, user, password);
statement = connection.createStatement();
}
}
}
return connection;
}
private static void CloseConnection() {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (ps != null) {
ps.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void ModifyData(String db, String data) throws Exception {
connectToDB(db);
try {
statement.execute(data);
} catch (SQLException e) {
e.printStackTrace();
} finally {
CloseConnection();
}
}
public static List> readData(String db, String sql) throws Exception {
List> list = new ArrayList<>();
int count;
connectToDB(db);
try {
rs = statement.executeQuery(sql);
ResultSetMetaData rsmd;
rsmd = rs.getMetaData();
count = rsmd.getColumnCount();
while (rs.next()) {
Map map = null;
for (int i = 1; i <= count; i++) {
map = new HashMap<>();
map.put(rsmd.getColumnLabel(i), rs.getString(i));
list.add(map);
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
CloseConnection();
}
return list;
}
}
總結
以上是生活随笔為你收集整理的java mysql 线程安全_java连接mysql的线程安全问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php 非阻塞mysql_php多进程中
- 下一篇: linux mysql安装失败 lib冲