Package com.killcoding.datasource
Class DriverDataSource
java.lang.Object
com.killcoding.datasource.DriverDataSource
- All Implemented Interfaces:
Wrapper,CommonDataSource,DataSource
Implement and enhance the datasource class
Support write connection status log
Support abort tiemout connection
Support failover
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Stringprivate Fileprivate Stringprivate booleanprivate ConfigPropertiesprivate final ConnectionPoolprivate final ExecutorServiceprivate Stringprivate longprivate longprivate longprivate longprivate longprivate longprivate longprivate Stringprivate Stringprivate Stringprivate longprivate Booleanprivate Booleanprivate Longprivate booleanprivate Longprivate booleanprivate final Loggerprivate Integerprivate Integerprivate doubleprivate Integerprivate Stringprivate Stringprivate Stringprivate final longprivate Longprivate Integerprivate Fileprivate Stringprivate List<DriverDataSource>private Longprivate Longprivate Longprivate Stringprivate intprivate booleanprivate String -
Constructor Summary
ConstructorsConstructorDescriptionNew a DriverDataSource object Use env APP_DATASOURCE=/path/DataSource.propertiesDriverDataSource(ConfigProperties configProperties) New a DriverDataSource objectDriverDataSource(File appDataSource) New a DriverDataSource objectDriverDataSource(String configPropertiesContent) New a DriverDataSource object -
Method Summary
Modifier and TypeMethodDescriptionprotected voidAbort connection by hash codeprivate voidAbort idle timeout connection Skip execute when 'idleTimeout' is 0private voidAbort invalid connection Skip execute when 'testValidTimeout' is 0private voidAbort life cycle connection Skip execute when 'lifeCycle' is 0private voidAbort timeout connection Skip execute when 'timeout' is 0 or 'transactionTimeout' is 0private voidaddCountAbortedCloseFailed(int value) Add count abort close failed for write status logprivate voidArchive logprivate voidcallAbortConn(Connection conn) Call abort connection Please see 'abortMode' options (rollback,close,abort) Default is 'abort'private voidvoidclose()Close all connectionsvoidcloseAll()Loop close all connectionsprotected voidcloseConnection(Integer hashCode) Close connectionprotected voidcloseConnection(Connection conn) Close connectionprivate voidcountUsage(boolean calAve) private voidCreate connection to pool When, (loadPoolSize + usedPoolSize) < maxPoolSize and loadPoolSize < minPoolSizeprivate voiddeleteFilesOlderThan(File directory, long fileOutArchiveDaysMs) Delete old archive logstatic voidDeregister JDBC Driver For system exitGet abort modeGet app datasource properties file pathdoubleGet config propertiesGet connectiongetConnection(String arg0, String arg1) private ConnectionGet connection from driverGet connection pool sizeprivate PropertiesGet connection propertiesGet connection propertiesGet driver class nameGet jdbc driver jar pathFor checksum master and sync data source connection url Pause sync where same connection urlGet idle timeoutGet connection life cycleintGet max connection pool sizeGet min connection pool sizegetName()Get thread nameprivate IntegergetOwner()Get owner thread name For use sync DataSourceGet refresh pool timerprotected final List<DriverDataSource>getSyncDataSourceList(String owner) Get sync data source listGet test valid timeoutGet execute timeoutGet execute transaction timeoutgetUrl()Get connection urldoublegetUsage()Get pool usingprivate voidinitDs()booleanCheck is loaded configbooleanisWrapperFor(Class arg0) private voidLoad connection to poolprivate DriverloadJdbcDriver(String driverJar, String driver) Load jdbc driver jarprivate voidManage connection poolprivate voidReload config When 'appDataSource' is file objectvoidsetAbortMode(String abortMode) Set abort modevoidsetAppDataSourcePath(String appDataSourcePath) Set app datasource properties file pathvoidsetConfig(ConfigProperties _configProperties) Set configvoidSet configvoidsetConnProps(String connProps) Set connection propertiesvoidsetDriverClassName(String driverClassName) Set driver class namevoidsetDriverJar(String driverJar) Set jdbc driver jar pathvoidsetEnableFullStatusLog(Boolean enableFullStatusLog) Set enable full status logvoidsetEnableStatusLog(Boolean enableStatusLog) Set enable status logvoidsetIdleTimeout(Long idleTimeout) Set idle timeoutprivate voidsetInternalConfig(ConfigProperties _configProperties) Set internal configprivate voidsetInternalConfig(String configPropertiesContent) Set internal configvoidsetLifeCycle(Long lifeCycle) Set connection life cyclevoidsetLoginTimeout(int arg0) voidsetLogWriter(PrintWriter arg0) voidsetMaxAveUsageSize(Integer maxAveUsageSize) voidsetMaxPoolSize(Integer maxPoolSize) Set max connection pool sizevoidsetMinPoolSize(Integer minPoolSize) Set min connection pool sizevoidSet thread namevoidsetPassword(String password) Set database passwordvoidsetRefreshPoolTimer(Long refreshPoolTimer) Set refresh pool timervoidsetStatusLogArchiveDays(Integer statusLogArchiveDays) Set status log archive daysvoidsetStatusLogFolder(String _statusLogFolder) Set status log target foldervoidsetTestValidTimeout(Long testValidTimeout) Set test valid timeoutvoidsetTimeout(Long timeout) Set execute timeout Default is 0s Disabled timeou if value is 0svoidsetTransactionTimeout(Long transactionTimeout) Set execute transaction timeout Default is 0s Disabled timeou if value is 0svoidSet connection urlvoidsetUsername(String username) Set database usernameprivate voidWrite connection status logprivate voidprivate ConnectiontryGetConnection(Driver driver, Properties props) Try get connection Support multi urlAbandon the following methodsMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface javax.sql.CommonDataSource
createShardingKeyBuilder
-
Field Details
-
log
-
DATASOURCE_ID
-
PID
-
countCreatedConnection
-
countAbortedIdle
-
countAbortedTimeout
-
countAbortedLifeCycle
-
countAbortedInvalid
-
countAbortedCloseFailed
-
countClosed
-
maxUsage
-
DRIVER_MARK
-
CONN_ERROR_TIME
-
inited
-
enableStatusLog
-
enableFullStatusLog
-
statusLogArchiveDays
-
statusLogFolder
-
statusLogFile
-
maxAveUsageSize
-
aveUsageSum
-
urlList
-
urlIndex
-
maxPoolSize
-
minPoolSize
-
lifeCycle
-
idleTimeout
-
timeout
-
transactionTimeout
-
refreshPoolTimer
-
testValidTimeout
-
configProperties
-
driverJar
-
connProps
-
driverClassName
-
url
-
username
-
password
-
name
-
abortMode
-
appDataSourcePath
-
useExternalConfig
-
loadedConfig
-
closed
-
appDataSource
-
owner
-
syncDriverDataSource
-
connectionPool
-
connectionPoolExecutor
-
dynamicReloadConfigTime
-
-
Constructor Details
-
DriverDataSource
public DriverDataSource()New a DriverDataSource object Use env APP_DATASOURCE=/path/DataSource.properties -
DriverDataSource
New a DriverDataSource object- Parameters:
appDataSource- - It is file object (e.g. /your_path/DataSource.properties)
-
DriverDataSource
New a DriverDataSource object- Parameters:
configPropertiesContent- - It is properties context- Throws:
IOException
-
DriverDataSource
New a DriverDataSource object- Parameters:
configProperties- - It is ConfigProperties object- Throws:
IOException
-
-
Method Details
-
initDs
-
setAppDataSourcePath
Set app datasource properties file path- Parameters:
appDataSourcePath- - file path
-
getAppDataSourcePath
Get app datasource properties file path -
isLoadedConfig
Check is loaded config- Returns:
- boolean
-
setEnableStatusLog
Set enable status log- Parameters:
enableStatusLog- - 'true' is enable write status log
-
setEnableFullStatusLog
Set enable full status log- Parameters:
enableFullStatusLog- - 'true' is enable write status log when full connections
-
setStatusLogArchiveDays
Set status log archive days- Parameters:
statusLogArchiveDays- - Delete before x days log
-
setStatusLogFolder
Set status log target folder- Parameters:
_statusLogFolder- - Target folder
-
setLifeCycle
Set connection life cycle- Parameters:
lifeCycle- - Unit is ms
-
getLifeCycle
Get connection life cycle- Returns:
- Long - Unit is ms
-
setRefreshPoolTimer
Set refresh pool timer- Parameters:
refreshPoolTimer- - Unit is ms
-
getRefreshPoolTimer
Get refresh pool timer- Returns:
- Long - Unit is ms
-
setTestValidTimeout
Set test valid timeout- Parameters:
testValidTimeout- Unit is seconds,default is 5s
-
getTestValidTimeout
Get test valid timeout -
setIdleTimeout
Set idle timeout- Parameters:
idleTimeout- - Unit is seconds, default is 60s
-
getIdleTimeout
Get idle timeout- Returns:
- Long - Unit is seconds
-
setTimeout
Set execute timeout Default is 0s Disabled timeou if value is 0s- Parameters:
timeout- - Unit is seconds,
-
getTimeout
Get execute timeout- Returns:
- Long - Unit is seconds,
-
setTransactionTimeout
Set execute transaction timeout Default is 0s Disabled timeou if value is 0s- Parameters:
idleTimeout- - Unit is seconds,
-
getTransactionTimeout
Get execute transaction timeout- Returns:
- Long - Unit is seconds,
-
setMaxPoolSize
Set max connection pool size- Parameters:
maxPoolSize-
-
getMaxPoolSize
Get max connection pool size- Returns:
- Integer
-
setDriverJar
Set jdbc driver jar path- Parameters:
driverJar- - Driver jar file or folder path
-
getDriverJar
Get jdbc driver jar path- Returns:
- String
-
setConnProps
Set connection properties- Parameters:
connProps- - Sample: autoReconnection=true;key=value
-
getConnProps
Get connection properties- Returns:
- connProps - Sample: autoReconnection=true;key=value
-
setDriverClassName
Set driver class name- Parameters:
driverClassName-
-
getDriverClassName
Get driver class name- Returns:
- String
-
setUrl
Set connection url- Parameters:
urlOrList- - String or List
-
getUrl
Get connection url -
setMinPoolSize
Set min connection pool size- Parameters:
minPoolSize-
-
getMinPoolSize
Get min connection pool size -
setAbortMode
Set abort mode- Parameters:
abortMode- - 'close' or 'abort', default is 'abort'
-
getAbortMode
Get abort mode- Returns:
- abortMode - 'close' or 'abort', default is 'abort'
-
setName
Set thread name- Parameters:
name- - It is thread name, default is 'DriverDataSource'
-
getName
Get thread name- Returns:
- String
-
setUsername
Set database username- Parameters:
username- - Database username
-
setPassword
Set database password- Parameters:
password- - Database password
-
getOpenConnectionSize
-
getConnectionPool
Get connection pool size- Returns:
- Integer
-
abortTimeoutConnection
Abort timeout connection Skip execute when 'timeout' is 0 or 'transactionTimeout' is 0 -
abortLifeCycleEndedConnection
Abort life cycle connection Skip execute when 'lifeCycle' is 0 -
abortIdleTimeoutConnection
Abort idle timeout connection Skip execute when 'idleTimeout' is 0 -
abortInvalidConnection
Abort invalid connection Skip execute when 'testValidTimeout' is 0 -
createConnectionToPool
Create connection to pool When, (loadPoolSize + usedPoolSize) < maxPoolSize and loadPoolSize < minPoolSize -
setInternalConfig
Set internal config- Parameters:
_configProperties-
-
setInternalConfig
Set internal config- Parameters:
configPropertiesContent-- Throws:
IOException
-
setConfig
Set config- Parameters:
configPropertiesContent-- Throws:
IOException
-
setConfig
Set config- Parameters:
_configProperties-- Throws:
IOException
-
reloadConfig
Reload config When 'appDataSource' is file object -
archiveLog
Archive log -
deleteFilesOlderThan
Delete old archive log- Throws:
IOException
-
statusLogOutput
Write connection status log -
manageConnectionPool
Manage connection pool -
getConnection
Get connection- Specified by:
getConnectionin interfaceDataSource- Returns:
- Connection
- Throws:
SQLException
-
throwConnIsNull
- Throws:
SQLException
-
checkConnectionPoolSize
- Throws:
SQLException
-
getConnectionProperties
Get connection properties- Returns:
- Properties
-
getConnectionFromDriver
Get connection from driver- Returns:
- Connection
- Throws:
SQLException
-
loadConnectionToPool
Load connection to pool- Throws:
SQLException
-
closeConnection
Close connection- Parameters:
conn- - Connection object- Throws:
SQLException
-
closeConnection
Close connection- Parameters:
hashCode- - Connection hash code- Throws:
SQLException
-
addCountAbortedCloseFailed
Add count abort close failed for write status log- Parameters:
value- - Change value
-
close
Close all connections -
closeAll
Loop close all connections -
deregisterDriver
Deregister JDBC Driver For system exit -
tryGetConnection
Try get connection Support multi url- Parameters:
driver- - Driverprops- - Connection properties ,var name is 'connProps'
-
abort
Abort connection by hash code- Parameters:
hashCode- - Connection hash code
-
callAbortConn
Call abort connection Please see 'abortMode' options (rollback,close,abort) Default is 'abort'- Parameters:
conn- - Connection
-
loadJdbcDriver
Load jdbc driver jar- Parameters:
driverJar- - It is jar file path or jar dir pathdriver-- Returns:
- Driver
- Throws:
SQLException
-
getOwner
Get owner thread name For use sync DataSource- Returns:
- String
-
getConfigProperties
Get config properties- Returns:
- ConfigProperties
-
getSyncDataSourceList
Get sync data source list- Parameters:
owner- - From master data source thread name- Returns:
- List
- Throws:
SQLException
-
getFingerprint
For checksum master and sync data source connection url Pause sync where same connection url- Returns:
- String
-
getAveUsage
-
setMaxAveUsageSize
-
countUsage
-
getUsage
Get pool using- Returns:
- double
-
unwrap
Abandon the following methods- Specified by:
unwrapin interfaceWrapper- Throws:
SQLException
-
isWrapperFor
- Specified by:
isWrapperForin interfaceWrapper- Throws:
SQLException
-
getParentLogger
- Specified by:
getParentLoggerin interfaceCommonDataSource- Throws:
SQLFeatureNotSupportedException
-
setLogWriter
- Specified by:
setLogWriterin interfaceCommonDataSource- Specified by:
setLogWriterin interfaceDataSource- Throws:
SQLException
-
getLogWriter
- Specified by:
getLogWriterin interfaceCommonDataSource- Specified by:
getLogWriterin interfaceDataSource- Throws:
SQLException
-
getLoginTimeout
- Specified by:
getLoginTimeoutin interfaceCommonDataSource- Specified by:
getLoginTimeoutin interfaceDataSource- Throws:
SQLException
-
setLoginTimeout
- Specified by:
setLoginTimeoutin interfaceCommonDataSource- Specified by:
setLoginTimeoutin interfaceDataSource- Throws:
SQLException
-
getConnection
- Specified by:
getConnectionin interfaceDataSource- Throws:
SQLException
-
createConnectionBuilder
- Specified by:
createConnectionBuilderin interfaceDataSource- Throws:
SQLException
-