使用相同连接的不同客户端mysql JSP

问题描述:

我有一个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(); 
    } 
} 
+0

请使用像[DBCP](http://commons.apache.org/proper/commons-dbcp/)这样的连接池,然后从中获取连接。你需要在你的web容器上配置它。 – 2015-02-10 18:45:43

你在找什么是连接池。这是一个有限的连接池,客户可以在需要时连接,并在完成后重新连接。这可以节省您的开销或始终创建和销毁连接。它还确保连接数量的增长可预测。

大多数容器提供这样的设施,例如,这里是documentation for configuring a connection pool in Tomcat。为您的容器找到一个。

如果由于某种原因您不能使用该容器或不希望容器负责,则可以使用一个库来帮助您在应用程序中自行管理连接池。在网络上,c3p0是很受欢迎的许多examples

编辑: Hikari是连接池的新酷选择。