Skip to content

Commit

Permalink
fixes #970 (#975)
Browse files Browse the repository at this point in the history
  • Loading branch information
GavinChenYan authored Jun 29, 2021
1 parent a2a5f19 commit 83a8950
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 3 deletions.
100 changes: 100 additions & 0 deletions data-source/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
## data-source module

The module provides datasources for different databases. When API need process database with data retrieve and data persistent, setting datasource will be the start point for the API.

### Implementation detail:

light-4j data-source module use lightweight java DB connection library (HikariCP) as datasource base:

```
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
```
data-source module defined a generic datasource(GenericDataSource) pre-defined several datasources for major databases (extend GenericDataSource):

- MysqlDataSource
- PostgresDataSource
- MariaDataSource
- OracleDataSource
- SqlServerDataSource
- H2DataSource

If user need define new datasources for other databases, simply create a new datasource class which extend GenericDataSource.
And if user want to customize the pre-defined datasource with other config values, we can extend the pre-defined datasource and add customized config for the datasource.
Please refer to CustomMysqlDataSource in test package

### Module usage detail:

There are two config files involve withe the setting (datasource.yml & service.yml) and both two be extend to use values.yml"

For example:

datasource.yml

```
MysqlDataSource: ${datasource.MysqlDataSource:}
```

service.yml

```
singletons: ${service.singletons:}
```

values.yml

```
# Service Singletons
service.singletons:
- com.networknt.decrypt.Decryptor:
- com.networknt.decrypt.AESDecryptor
- com.networknt.db.GenericDataSource:
- com.networknt.db.MysqlDataSource:
- java.lang.String:
- com.networknt.accountservic.dao.AccountDao:
- com.networknt.accountservic.dao.AccountDaoImpl
# datasource.yml
datasource.MysqlDataSource:
DriverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3308/account_db?useSSL=false
username: account_user
password: CRYPT:odPqWOazjDxeVcOU3j0YCc2+LdwfgiJmoFcWTSoKRUw=
maximumPoolSize: 2
connectionTimeout: 5000
parameters:
useServerPrepStmts: 'true'
cachePrepStmts: 'true'
cacheCallableStmts: 'true'
prepStmtCacheSize: '4096'
prepStmtCacheSqlLimit: '2048'
verifyServerCertificate: 'false'
useSSL: 'true'
requireSSL: 'true'
```

DAO java class:

```
private DataSource dataSource;
public AccountDaoImpl(GenericDataSource genericDataSource) {
dataSource = genericDataSource.getDataSource();
}
```


### For the detail document, pleases refer to

* [Tutorial document](https://doc.networknt.com/concern/datasource/#readout) with step by step guide to set and use datasources
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class GenericDataSource {
private HikariDataSource ds;
// the data source name
protected String dsName;
protected Map<String, Object> dataSourceMap;

public String getDsName() {
return dsName;
Expand All @@ -60,7 +61,7 @@ public GenericDataSource(String dsName) {

protected HikariDataSource createDataSource() {
// get the configured datasources
Map<String, Object> dataSourceMap = Config.getInstance().getJsonMapConfig(DATASOURCE);
dataSourceMap = Config.getInstance().getJsonMapConfig(DATASOURCE);
// get the requested datasource
Map<String, Object> mainParams = (Map<String, Object>) dataSourceMap.get(getDsName());
Map<String, String> configParams = (Map<String, String>)mainParams.get("parameters");
Expand Down Expand Up @@ -88,9 +89,9 @@ protected HikariDataSource createDataSource() {
/**
* Get an instance of the datasource
*
* @return the DataSource object
* @return the HikariDataSource object
*/
public DataSource getDataSource() {
public HikariDataSource getDataSource() {
return ds;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2016 Network New Technologies Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.networknt.db;

import java.util.Map;

/**
* Sample customized the MysqlDataSource by adding extended config
*
* @author Gavin Chen
*/
public class CustomMysqlDataSource extends MysqlDataSource {

private static final String CUSTOM_MYSQL_DS = "MysqlDataSource";
private static final String LEAK_DETECTION_THRESHOLD = "leakDetectionThreshold";
private static final String REGISTER_MBEANS = "registerMBeans";
private static final String CONNECTION_TEST_QUERY = "testQuery";


@Override
public String getDsName() {
if(dsName != null) {
return dsName;
} else {
return CUSTOM_MYSQL_DS;
}
}

public CustomMysqlDataSource(String dsName) {
super(dsName);
setExtendConfigParams();
}

public CustomMysqlDataSource() {
super();
setExtendConfigParams();
}

public void setExtendConfigParams() {
Map<String, Object> mainParams = (Map<String, Object>) dataSourceMap.get(getDsName());
if (mainParams.containsKey(LEAK_DETECTION_THRESHOLD)) this.getDataSource().setLeakDetectionThreshold((Integer)mainParams.get(LEAK_DETECTION_THRESHOLD));
if (mainParams.containsKey(REGISTER_MBEANS)) this.getDataSource().setRegisterMbeans((Boolean) mainParams.get(REGISTER_MBEANS));
if (mainParams.containsKey(CONNECTION_TEST_QUERY)) this.getDataSource().setConnectionTestQuery((String)mainParams.get(CONNECTION_TEST_QUERY));

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public void testGetH2DataSource() {
}

@Test
@Ignore
public void testGetMysqlDataSource() {
DataSource ds = SingletonServiceFactory.getBean(MysqlDataSource.class).getDataSource();
assertNotNull(ds);
Expand All @@ -71,6 +72,19 @@ public void testGetMysqlDataSource() {
}
}

@Test
@Ignore
public void testCustomMysqlDataSource() {
DataSource ds = SingletonServiceFactory.getBean(CustomMysqlDataSource.class).getDataSource();
assertNotNull(ds);

try(Connection connection = ds.getConnection()){
assertNotNull(connection);
} catch (SQLException e) {
e.printStackTrace();
}
}

@Test
@Ignore
public void testGetMariaDataSource() {
Expand Down
21 changes: 21 additions & 0 deletions data-source/src/test/resources/config/datasource.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,27 @@ MysqlDataSource:
verifyServerCertificate: 'false'
useSSL: 'true'
requireSSL: 'true'

CustomMysqlDataSource:
DriverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3308/account_db?useSSL=false
username: account_user
password: passWord00
maximumPoolSize: 2
connectionTimeout: 500
leakDetectionThreshold: 100
registerMBeans: true
testQuery: select version()
parameters:
useServerPrepStmts: 'true'
cachePrepStmts: 'true'
cacheCallableStmts: 'true'
prepStmtCacheSize: '4096'
prepStmtCacheSqlLimit: '2048'
verifyServerCertificate: 'false'
useSSL: 'true'
requireSSL: 'true'

MariaDataSource:
DriverClassName: org.mariadb.jdbc.MariaDbDataSource
jdbcUrl: jdbc:mariadb://mariadb:3306/oauth2?useSSL=false
Expand Down
4 changes: 4 additions & 0 deletions data-source/src/test/resources/config/service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ singletons:
- com.networknt.db.MysqlDataSource:
- java.lang.String: MysqlDataSource

- com.networknt.db.CustomMysqlDataSource:
- com.networknt.db.CustomMysqlDataSource:
- java.lang.String: CustomMysqlDataSource

- com.networknt.db.MariaDataSource:
- com.networknt.db.MariaDataSource:
- java.lang.String: MariaDataSource
Expand Down

0 comments on commit 83a8950

Please sign in to comment.