0
我有一個JPS項目。使用相同連接的不同客戶端mysql JSP
如果我有不同的計算機使用系統,他們使用相同的MySQL連接。
當系統運行任何查詢並且客戶端嘗試發出任何mysql命令時,它將所有人放入隊列中,系統非常慢。
我希望每個客戶端都與mysql有不同的連接。
對不起,如果我不夠清楚。
package br.com.scope.model;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import br.com.scope.log.LoggerIntegrador;
public class ConexaoMysql {
private static Connection connection;
private final static ConexaoMysql conexaoMysql = new ConexaoMysql();
private static Properties prop;
private LoggerIntegrador logger;
private ConexaoMysql() {
super();
prop = new Properties();
Class<? extends ConexaoMysql> cls = this.getClass();
InputStream is = cls.getResourceAsStream("db.properties");
try {
prop.load(is);
} catch (IOException e) {
logger = LoggerIntegrador.getInstance();
logger.error(e.getMessage(), e);
}
}
public static ConexaoMysql getConnection() throws SQLException, ClassNotFoundException, IOException {
if (connection == null || connection.isClosed()){
conexaoMysql.abreConexao();
}
return conexaoMysql;
}
public static void beginTransaction() throws SQLException, ClassNotFoundException, IOException {
getConnection();
connection.setAutoCommit(false);
}
public static void commit() throws SQLException {
connection.commit();
connection.setAutoCommit(true);
}
public static String getDriver() {
return prop.getProperty("driver");
}
public static String getConnectionString() {
return prop.getProperty("connectionstring");
}
public static String getUser() {
return prop.getProperty("user");
}
public static String getPassword() {
return prop.getProperty("password");
}
private void abreConexao() throws ClassNotFoundException, SQLException, IOException{
Class.forName(getDriver());
connection = DriverManager.getConnection(
getConnectionString(),
getUser(),
getPassword());
}
public static void fechaConexao() throws SQLException {
if (!connection.isClosed()) {
connection.close();
}
}
public PreparedStatement getPreparedStatement(String sql) throws SQLException {
return connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
}
public static int getId() {
return conexaoMysql.hashCode();
}
}
請使用像[DBCP](http://commons.apache.org/proper/commons-dbcp/)這樣的連接池,然後從中獲取連接。你需要在你的web容器上配置它。 – 2015-02-10 18:45:43