DELL
2024-06-26 65ff6cbca071027f564ebb8fb5c713534c8bfca7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.dec.fbs9100;
 
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
import com.mchange.v2.c3p0.ComboPooledDataSource;
 
public class MysqlConnPool {
 
    private ComboPooledDataSource mysql_ds;// = new ComboPooledDataSource();
    private int mSqlPort = 3360;
    public Logger logger = null;
        
    public MysqlConnPool(String server_ip, int port, int conncount_max)
    {
        logger = LogManager.getLogger(this.getClass());
        
        try {
            logger.info("sqlserver_ip:" + server_ip + ", port:" + port + ", conn_max:" + conncount_max);
            mysql_ds = new ComboPooledDataSource();
            init(server_ip, port, conncount_max);
        } catch (PropertyVetoException e) {
            logger.error(e.toString(), e);
        }
    }
    public void init(String server_ip, int port, int conncount_max) throws PropertyVetoException
    {
        mSqlPort = port;
        mysql_ds.setTestConnectionOnCheckout(false);
        mysql_ds.setDriverClass("com.mysql.jdbc.Driver");
        mysql_ds.setJdbcUrl("jdbc:mysql://" + server_ip + ":" + mSqlPort + "/db_app_sys");
        mysql_ds.setUser("root");
        mysql_ds.setPassword("lmx8688139");
        mysql_ds.setMaxPoolSize(conncount_max);
        mysql_ds.setMinPoolSize(2);
        mysql_ds.setAcquireIncrement(4);
        mysql_ds.setMaxIdleTime(120);
        mysql_ds.setNumHelperThreads(4);
        mysql_ds.setBreakAfterAcquireFailure(true);
        mysql_ds.setIdleConnectionTestPeriod(60);
    }
    public Connection getConn()
    {    
        Connection con = null;
        try {
            con = mysql_ds.getConnection();
        } catch (SQLException e) {
            //e.printStackTrace();
            logger.error(e.toString(), e);
        }
        return con;
    }
    
    public int getSqlConnPort() {
        return mSqlPort;
    }
    
    public int getMaxPoolSize() {
        return (mysql_ds.getMaxPoolSize());
    }
    
    public void setMaxPoolSize(int size) {
        mysql_ds.setMaxPoolSize(size);
    }
    
    public static void main(String[] args) {
        MysqlConnPool pool = new  MysqlConnPool("127.0.0.1", 3360, 50);
        System.out.println(pool.getConn());
    }
}