P15GEN2\59518
2025-10-10 9f6890646993d16260d4201d613c092132856127
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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;
import foundation.util.Util;
 
public class NamedDataSource extends DruidDataSource implements IJSONProvider {
 
    private static final long serialVersionUID = 1L;
    private String name;
    private DBaseType dbaseType;
    private String version;
    
    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;
    }
 
    public void setConfigVesion(String version) {
        this.version = version;
        
        if (dbaseType != null) {
            dbaseType.setVersion(version);
        }
    }
 
    public String getConfigVersion() {
        return version;
    }
    
    @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, version);
    }
    
    public String getName() {
        return name;
    }
 
    public DBaseType getDBaseType() {
        return dbaseType;
    }
    
    public boolean keyEquals(String sourceKey) {
        return this.name.equalsIgnoreCase(sourceKey);
    }
 
    public String getSchema() {
        if (Util.isEmpty(jdbcUrl)) {
            return null;
        }
        try {
            int startIndex = jdbcUrl.lastIndexOf("/", jdbcUrl.lastIndexOf("/") + 1) + 1;
            int endIndex = jdbcUrl.indexOf("?", startIndex);
            String databaseName = jdbcUrl.substring(startIndex, endIndex);
            // 提取database name(去掉前面的/)
//            String databaseName = path.substring(1);
            return databaseName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    @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();
    }
}