P15GEN2\59518
2024-05-29 d4210c7c4b04abde20037ea8aa0f54ef8a2649aa
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package foundation.persist.source;
 
import java.sql.Connection;
import java.sql.SQLException;
 
import com.alibaba.druid.pool.DruidDataSource;
 
import foundation.json.IJSONProvider;
import foundation.json.IJSONWriter;
import foundation.server.config.DBaseType;
 
public class NamedDataSource extends DruidDataSource implements IJSONProvider {
 
    private static final long serialVersionUID = 1L;
    private String name;
    private DBaseType dbaseType;
    
    public NamedDataSource(String name) throws SQLException {
        this.name = name;
        
        setInitialSize(1);
        setFilters("stat,log4j2");
        setMaxWait(60000);
        setMinIdle(1);
        setTimeBetweenEvictionRunsMillis(3000);
        setMinEvictableIdleTimeMillis(3000 * 500);
        setRemoveAbandonedTimeout(300);
        setTestWhileIdle(true);
        setTestOnBorrow(false);
        setTestOnReturn(false);
        
        dbaseType = DBaseType.Unknown;
    }
    
    public static NamedDataSource getInstance() {
        return DataSourceManager.getMain();
    }
    
    public static NamedDataSource getInstance(String name) {
        NamedDataSource result = DataSourceManager.getDataSource(name);
        return result;
    }
    
    @Override
    public void setUrl(String url) {
        super.setUrl(url);
        
        if (url == null) {
            return;
        }
        
        url = url.toLowerCase();
        if (url.indexOf("oracle") > 0) {
            dbaseType = DBaseType.Oracle;
            setValidationQuery("select 1 from dual");
        }
        else if (url.indexOf("mysql") > 0) {
             dbaseType = DBaseType.MySQL;
            setValidationQuery("select 1");
        }
        else if (url.indexOf("sqlserver") > 0) {
            dbaseType = DBaseType.SQLServer;
            setValidationQuery("select 1");
        }
        else {
            dbaseType = DBaseType.Unknown;
            setValidationQuery("select 1");            
        }
    }
 
    public ConnectionAgent getConnectionAgent() throws SQLException {
        Connection conn = getConnection();
        return new ConnectionAgent(name, conn, dbaseType);
    }
    
    public String getName() {
        return name;
    }
 
    public DBaseType getDBaseType() {
        return dbaseType;
    }
 
    public boolean keyEquals(String sourceKey) {
        return this.name.equalsIgnoreCase(sourceKey);
    }
 
    @Override
    public void writeJSON(IJSONWriter writer) {
        writer.beginArray();
 
        writer.write("name", getName());
        writer.write("dbType", getDbType());
        
        writer.write("connectCount", getConnectCount());
        writer.write("activeCount", getActiveCount());
        writer.write("closeCount", getCloseCount());
        writer.write("destroyCount", getDestroyCount());
        writer.write("scardCount", getDiscardCount());
        writer.write("activePeak", getActivePeak());
        writer.write("createTime", getCreatedTime());
        
        writer.endArray();
    }
}