Administrator
2021-11-26 0deb53a184ea4bb5ca6967b2d9342cb597b53ab2
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
75
76
77
78
79
80
81
82
83
84
85
86
package com.battmonitor.sql;
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 = 5306;
    
    public Logger logger = null;
    
    public MysqlConnPool(String server_ip, int port, int conncount_max)
    {
        logger = LogManager.getLogger(this.getClass());
        
        try {
            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.setDriverClass("com.mysql.jdbc.Driver");
        mysql_ds.setJdbcUrl("jdbc:mysql://" + server_ip + ":" + mSqlPort);
        mysql_ds.setUser("root");
        mysql_ds.setPassword("lmx8688139");
        mysql_ds.setMaxPoolSize(conncount_max);
        mysql_ds.setMinPoolSize(2);
        mysql_ds.setIdleConnectionTestPeriod(300);
    }
    public MysqlConnPool getConn()
    {
        return this;
    }
    public Connection getMysqlConn()
    {
        Connection con = null;
        try {
            con = mysql_ds.getConnection();
        } catch (SQLException e) {
            logger.error(e.toString(), e);
        }
        return con;
    }
    
    public int getSqlConnPort() {
        return mSqlPort;
    }
    
    public int getNumBusyConnections() {
        int cnt = 0;
        try {
            cnt = mysql_ds.getNumBusyConnections();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            logger.error(e.toString(), e);
        }
        return cnt;
    }
    
    public int getNumIdleConnections() {
        int cnt = 0;
        try {
            cnt = mysql_ds.getNumIdleConnections();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            logger.error(e.toString(), e);
        }
        return cnt;
    }
    
    public int getMaxPoolSize() {
        return (mysql_ds.getMaxPoolSize());
    }
    
    public void setMaxPoolSize(int size) {
        mysql_ds.setMaxPoolSize(size);
    }
}