简体   繁体   English

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

[英]Different clients using same connection mysql JSP

I have an JPS Project. 我有一个JPS项目。

If I have different computers using the system, they use the same MySQL connection. 如果我使用该系统的计算机不同,则它们使用相同的MySQL连接。

When the system is running any query and a client tries to make any mysql command, it puts everyone in a queue, the system is very slow. 当系统运行任何查询并且客户端尝试执行任何mysql命令时,它将所有人排在队列中,系统速度非常慢。

I want each client has a different connection with mysql. 我希望每个客户端与mysql都有不同的连接。

Sorry if I was not clear enough. 对不起,如果我不清楚。

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();
    }
}

What you're looking for is a connection pool. 您正在寻找的是连接池。 It is a bounded pool of connections that the clients can take a connection from when they need it, and put it back to when done. 它是一个有限的连接池,客户端可以在需要它们时将其从连接中取出,并在完成时将其放回连接中。 This saves you from the overhead or creating and destroying connections all the time. 这可以节省您的开销,也可以始终避免创建和销毁连接。 It also makes sure the number of connections grows predictably. 它还可以确保连接数量可预测地增长。

Most containers provide a facility like this eg here's the documentation for configuring a connection pool in Tomcat . 大多数容器都提供类似的功能,例如,这是在Tomcat中配置连接池文档 Find the one for your container. 找到一个适合您的容器。

If for some reason you can not use that or do not want the container to be in charge, you can use a library that helps you manage a connection pool yourself, in your application. 如果由于某种原因您不能使用它或不想让容器负责,则可以使用一个库来帮助您自己在应用程序中管理连接池。 c3p0 is a popular one with many examples on the web. c3p0是一个流行的示例 ,在网络上有很多示例

EDIT: Hikari is the new cool choice for connection pooling. 编辑: Hikari是连接池的新选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM