I have searched the Internet for N days, and there are almost no thread-safe solutions. The same problem can be easily solved with Redis. I changed it to a tool class found online. Please help me modify it or give me some advice. Guidance. My current idea is to add the synchronized keyword, but I always feel that there is still a problem. Thank you very much!
class MySQLUtil {
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 = "root";
private static final String password = "111111";
private static Connection connection = null;
private static Statement statement = null;
private static PreparedStatement ps = null;
private static ResultSet rs = null;
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;
}
synchronized private void ConnectToDB(String db) {
try {
Class.forName(driver);
Connection connection = DriverManager.getConnection(url + db + character + ssl, user, password);
if (!connection.isClosed()) {
statement = connection.createStatement();
}
} catch (Exception e) {
e.printStackTrace();
}
}
synchronized private 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();
}
}
synchronized void ModifyData(String db, String data) {
ConnectToDB(db);
try {
statement.execute(data);
} catch (SQLException e) {
e.printStackTrace();
} finally {
CloseConnection();
}
}
synchronized List ReadData(String db, String data) {
List<String> list = new ArrayList<>();
int count;
ConnectToDB(db);
try {
rs = statement.executeQuery(data);
ResultSetMetaData rsmd;
rsmd = rs.getMetaData();
count = rsmd.getColumnCount();
while (rs.next()) {
for (int i = 1; i <= count; i++) {
String label = rsmd.getColumnLabel(i);
list.add(label);
String value = rs.getString(i);
list.add(value);
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
CloseConnection();
}
return list;
}
}
In order to ensure that the data between connections is independent (non-shared), I guess you want to implement Connection pool:
After a slight modification, it may be better. It is recommended to listen to the buddy above and use a mature database connection pool. There is no need to reinvent the wheel
Use singleton to ensure the uniqueness of database connection
Modify the usage of
synchronized
keyword to improve efficiencyAdded
volatile
keyword to improve stabilityNo need to synchronize, multiple connections don’t matter.
The database has its own lock.
You can also use the connection pool directly.
Thank you everyone for your answers. I changed the code a bit. Please help me see if there is any problem. The main reason is that I have never done Java. My way of handling it is: except for constants, there are no class member variables. All parameters and The return value is passed and all variables are declared in the method