简体   繁体   English

java jdbc连接url中Mysql系统变量的更改不生效

[英]change in Mysql system variable in java jdbc connection url do not take effect

In mysql the system variable "max_connections" has value 100. But i am trying to override this value programatically by pass key-value pair in jdbc connection string as "jdbc:mysql://localhost/test?maxConnections=3". 在mysql中,系统变量“ max_connections”的值为100。但是我试图通过在jdbc连接字符串中将键值对传递为“ jdbc:mysql:// localhost / test?maxConnections = 3”,以编程方式覆盖此值。 But it seems like it is not reflecting any change. 但这似乎并没有反映任何变化。 Here below I made maxConnection to 3 but still in below example I am able to create 40 connections. 在下面,我将maxConnection设置为3,但在下面的示例中,我仍然可以创建40个连接。 Note I have not closed connection in each iteration purposely.Just to see if the key value added in jdbc connection string is taking effect.If it takes effect then it should give exception of "Too many Connections" 注意我没有在每次迭代中都故意关闭连接。只是看看添加在jdbc连接字符串中的键值是否生效。如果生效,则应给出“ Too many Connections”的异常

See below code for reference: 请参阅以下代码以供参考:

public class JDBCOne {

    public static void main(String[] args) throws ClassNotFoundException, SQLException {


        for(int i=1;i<=35;i++)
            {
            Connection conn = null;  
            Statement stmt = null;

            Class.forName("com.mysql.jdbc.Driver");

            System.out.println("Connecting to database...:"+i);
            conn = DriverManager.getConnection("jdbc:mysql://localhost/test?maxConnections=3", "root", "root");
            stmt = conn.createStatement();
            Integer id = null;
            String name = null;
            Double amount = null;
            String sql = "SELECT * FROM emp where id=1";

            ResultSet rs = stmt.executeQuery(sql);

            while (rs.next()) {
                id = rs.getInt("id");
                name = rs.getString("name");
                amount = rs.getDouble("amount");
            }
            System.out.println("id-" + id);
        }
        System.out.println("Goodbye!");
    }

}

max_connections is a global MySQL server variable governing the number of connections the server accepts from all programs connecting to it. max_connections是一个全局MySQL服务器变量,用于控制服务器从与其连接的所有程序中接受的连接数。

It's not a configuration string parameter, though. 但是,它不是配置字符串参数。 So, if you try to change it with a connection string, You Can't Do That™. 因此,如果您尝试使用连接字符串进行更改,则您将无法做到这一点。

See this. 看到这个。 https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html https://dev.mysql.com/doc/connector-j/5.1/zh-CN/connector-j-reference-configuration-properties.html

You can set it using set global max_connections = 200; 您可以使用set global max_connections = 200;进行set global max_connections = 200; if you have the SUPER privilege. 如果您拥有SUPER特权。

eg: 例如:

try (PreparedStatement pstmt = conn.prepareStatement("SET GLOBAL max_connections = ?")) {
    pstmt.setInt(1, 200);
    pstmt.execute();
} catch (SQLException ex) {
    throw new RuntimeException(ex);
}

I can't say for sure, but it may be possible that com.mysql.jdbc.Driver is closing some of the connections when conn and/or stmt go out of scope. 我不能肯定地说,但是当conn和/或stmt超出范围时, com.mysql.jdbc.Driver可能会关闭某些连接。

You could try putting them outside the for loop: 您可以尝试将它们放在for循环之外:

        Connection conn = null;  
        Statement stmt = null; 
        for(int i=1;i<=35;i++)
        {

You could verify if connections are staying open by running the following MySQL command: 您可以通过运行以下MySQL命令来验证连接是否保持打开状态:

SHOW PROCESSLIST;

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

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