1
1
package io .apicurio .registry .storage .impl .sql ;
2
2
3
3
import io .agroal .api .AgroalDataSource ;
4
- import io .agroal .api .configuration .AgroalConnectionFactoryConfiguration .TransactionIsolation ;
5
- import io .agroal .api .configuration .AgroalConnectionPoolConfiguration .TransactionRequirement ;
6
- import io .agroal .api .configuration .supplier .AgroalPropertiesReader ;
7
4
import io .apicurio .common .apps .config .Info ;
8
5
import jakarta .enterprise .context .ApplicationScoped ;
9
6
import jakarta .inject .Inject ;
13
10
import org .slf4j .Logger ;
14
11
15
12
import java .sql .SQLException ;
16
- import java .util .HashMap ;
17
- import java .util .Map ;
18
13
19
14
public class RegistryDatasourceProducer {
20
15
@@ -25,29 +20,21 @@ public class RegistryDatasourceProducer {
25
20
@ Info (category = "storage" , description = "Application datasource database type" , availableSince = "3.0.0" )
26
21
String databaseType ;
27
22
28
- @ ConfigProperty (name = "apicurio.datasource.url" , defaultValue = "jdbc:h2:mem:registry_db" )
29
- @ Info (category = "storage" , description = "Application datasource jdbc url" , availableSince = "3.0.0" )
30
- String jdbcUrl ;
31
-
32
- @ ConfigProperty (name = "apicurio.datasource.username" , defaultValue = "sa" )
33
- @ Info (category = "storage" , description = "Application datasource username" , availableSince = "3.0.0" )
34
- String username ;
35
-
36
- @ ConfigProperty (name = "apicurio.datasource.password" , defaultValue = "sa" )
37
- @ Info (category = "storage" , description = "Application datasource password" , availableSince = "3.0.0" )
38
- String password ;
23
+ @ Inject
24
+ @ Named ("h2" )
25
+ AgroalDataSource h2Datasource ;
39
26
40
- @ ConfigProperty ( name = "apicurio.datasource.jdbc.initial-size" , defaultValue = "20" )
41
- @ Info ( category = "storage" , description = "Application datasource pool initial size" , availableSince = "3.0.0 " )
42
- String initialSize ;
27
+ @ Inject
28
+ @ Named ( "postgresql " )
29
+ AgroalDataSource postgresqlDatasource ;
43
30
44
- @ ConfigProperty ( name = "apicurio.datasource.jdbc.min-size" , defaultValue = "20" )
45
- @ Info ( category = "storage" , description = "Application datasource pool minimum size" , availableSince = "3.0.0 " )
46
- String minSize ;
31
+ @ Inject
32
+ @ Named ( "mysql " )
33
+ AgroalDataSource mysqlDatasource ;
47
34
48
- @ ConfigProperty ( name = "apicurio.datasource.jdbc.max-size" , defaultValue = "100" )
49
- @ Info ( category = "storage" , description = "Application datasource pool maximum size" , availableSince = "3.0.0 " )
50
- String maxSize ;
35
+ @ Inject
36
+ @ Named ( "mssql " )
37
+ AgroalDataSource mssqlDatasource ;
51
38
52
39
@ Produces
53
40
@ ApplicationScoped
@@ -57,30 +44,24 @@ public AgroalDataSource produceDatasource() throws SQLException {
57
44
58
45
final RegistryDatabaseKind databaseKind = RegistryDatabaseKind .valueOf (databaseType );
59
46
60
- Map <String , String > props = new HashMap <>();
61
-
62
- props .put (AgroalPropertiesReader .MAX_SIZE , maxSize );
63
- props .put (AgroalPropertiesReader .MIN_SIZE , minSize );
64
- props .put (AgroalPropertiesReader .INITIAL_SIZE , initialSize );
65
- props .put (AgroalPropertiesReader .JDBC_URL , jdbcUrl );
66
- props .put (AgroalPropertiesReader .PRINCIPAL , username );
67
- props .put (AgroalPropertiesReader .CREDENTIAL , password );
68
- props .put (AgroalPropertiesReader .PROVIDER_CLASS_NAME , databaseKind .getDriverClassName ());
69
-
70
- /*
71
- * We need to disable auto-commit to have proper transaction rollback, otherwise the data may be in an
72
- * inconsistent state (e.g. partial deletes), or operations would not be rolled back on exception.
73
- */
74
- props .put (AgroalPropertiesReader .AUTO_COMMIT , "false" );
75
- props .put (AgroalPropertiesReader .TRANSACTION_ISOLATION , TransactionIsolation .READ_COMMITTED .name ());
76
- props .put (AgroalPropertiesReader .TRANSACTION_REQUIREMENT , TransactionRequirement .WARN .name ());
77
- props .put (AgroalPropertiesReader .FLUSH_ON_CLOSE , "true" );
78
-
79
- AgroalDataSource datasource = AgroalDataSource
80
- .from (new AgroalPropertiesReader ().readProperties (props ).get ());
81
-
82
47
log .info ("Using {} SQL storage." , databaseType );
83
48
84
- return datasource ;
49
+ switch (databaseKind ) {
50
+ case h2 -> {
51
+ return h2Datasource ;
52
+ }
53
+ case postgresql -> {
54
+ return postgresqlDatasource ;
55
+ }
56
+ case mysql -> {
57
+ return mysqlDatasource ;
58
+ }
59
+ case mssql -> {
60
+ return mssqlDatasource ;
61
+ }
62
+ default -> throw new IllegalStateException (
63
+ String .format ("unrecognized database type: %s" , databaseKind .name ()));
64
+ }
65
+
85
66
}
86
67
}
0 commit comments