Skip to content

Commit ee6a63c

Browse files
author
Jonathan Henrique Medeiros
committed
Minor fixes
1 parent ebc1364 commit ee6a63c

File tree

5 files changed

+81
-43
lines changed

5 files changed

+81
-43
lines changed

src/main/java/br/com/multidatasources/config/datasource/DataSourceConfiguration.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@
77

88
public interface DataSourceConfiguration {
99

10-
String getPoolName();
10+
String poolName();
1111

12-
int getMinimumIdle();
12+
int minimumIdle();
1313

14-
int getMaximumPoolSize();
14+
int maximumPoolSize();
1515

16-
long getConnectionTimeout();
16+
long connectionTimeout();
1717

18-
long getMaxLifetime();
18+
long idleTimeout();
19+
20+
long maxLifetime();
1921

2022
default HikariDataSource definePoolDataSourceConnection(final DataSource dataSource) {
2123
return new HikariDataSource(hikariConfig(dataSource));
@@ -24,11 +26,12 @@ default HikariDataSource definePoolDataSourceConnection(final DataSource dataSou
2426
private HikariConfig hikariConfig(final DataSource dataSource) {
2527
final HikariConfig hikariConfig = new HikariConfig();
2628

27-
hikariConfig.setPoolName(getPoolName());
28-
hikariConfig.setMaximumPoolSize(getMaximumPoolSize());
29-
hikariConfig.setMinimumIdle(getMinimumIdle());
30-
hikariConfig.setConnectionTimeout(getConnectionTimeout());
31-
hikariConfig.setMaxLifetime(getMaxLifetime());
29+
hikariConfig.setPoolName(poolName());
30+
hikariConfig.setMaximumPoolSize(maximumPoolSize());
31+
hikariConfig.setMinimumIdle(minimumIdle());
32+
hikariConfig.setConnectionTimeout(connectionTimeout());
33+
hikariConfig.setMaxLifetime(maxLifetime());
34+
hikariConfig.setIdleTimeout(idleTimeout());
3235
hikariConfig.setDataSource(dataSource);
3336
hikariConfig.setAutoCommit(false);
3437

src/main/java/br/com/multidatasources/config/datasource/DataSourceType.java

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,55 @@ public enum DataSourceType {
44

55
READ_ONLY("Replica-DB") {
66
@Override
7-
public int getMinimumIdle() {
7+
public int minimumIdle() {
88
return Runtime.getRuntime().availableProcessors();
99
}
1010

1111
@Override
12-
public int getMaximumPoolSize() {
12+
public int maximumPoolSize() {
1313
return Runtime.getRuntime().availableProcessors() * 4;
1414
}
1515

1616
@Override
17-
public long getConnectionTimeout() {
17+
public long connectionTimeout() {
1818
return 250;
1919
}
2020

2121
@Override
22-
public long getMaxLifetime() {
22+
public long idleTimeout() {
2323
return 600000;
2424
}
25+
26+
@Override
27+
public long maxLifetime() {
28+
return 800000;
29+
}
2530
},
2631
READ_WRITE("Master-DB") {
2732
@Override
28-
public int getMinimumIdle() {
33+
public int minimumIdle() {
2934
return Runtime.getRuntime().availableProcessors();
3035
}
3136

3237
@Override
33-
public int getMaximumPoolSize() {
38+
public int maximumPoolSize() {
3439
return Runtime.getRuntime().availableProcessors() * 2;
3540
}
3641

3742
@Override
38-
public long getConnectionTimeout() {
43+
public long connectionTimeout() {
3944
return 250;
4045
}
4146

4247
@Override
43-
public long getMaxLifetime() {
48+
public long idleTimeout() {
4449
return 600000;
4550
}
51+
52+
@Override
53+
public long maxLifetime() {
54+
return 800000;
55+
}
4656
};
4757

4858
private final String poolName;
@@ -51,16 +61,18 @@ public long getMaxLifetime() {
5161
this.poolName = poolName;
5262
}
5363

54-
public String getPoolName() {
64+
public String poolName() {
5565
return this.poolName;
5666
}
5767

58-
public abstract int getMinimumIdle();
68+
public abstract int minimumIdle();
69+
70+
public abstract int maximumPoolSize();
5971

60-
public abstract int getMaximumPoolSize();
72+
public abstract long connectionTimeout();
6173

62-
public abstract long getConnectionTimeout();
74+
public abstract long idleTimeout();
6375

64-
public abstract long getMaxLifetime();
76+
public abstract long maxLifetime();
6577

6678
}

src/main/java/br/com/multidatasources/config/datasource/MasterDataSourceConfiguration.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,40 @@
88

99
import javax.sql.DataSource;
1010

11+
import static br.com.multidatasources.config.datasource.DataSourceType.READ_ONLY;
1112
import static br.com.multidatasources.config.datasource.DataSourceType.READ_WRITE;
1213

1314
@Configuration
1415
public class MasterDataSourceConfiguration implements DataSourceConfiguration {
1516

1617
@Override
17-
public String getPoolName() {
18-
return READ_WRITE.getPoolName();
18+
public String poolName() {
19+
return READ_WRITE.poolName();
1920
}
2021

2122
@Override
22-
public int getMinimumIdle() {
23-
return READ_WRITE.getMinimumIdle();
23+
public int minimumIdle() {
24+
return READ_WRITE.minimumIdle();
2425
}
2526

2627
@Override
27-
public int getMaximumPoolSize() {
28-
return READ_WRITE.getMaximumPoolSize();
28+
public int maximumPoolSize() {
29+
return READ_WRITE.maximumPoolSize();
2930
}
3031

3132
@Override
32-
public long getConnectionTimeout() {
33-
return READ_WRITE.getConnectionTimeout();
33+
public long idleTimeout() {
34+
return READ_ONLY.idleTimeout();
3435
}
3536

3637
@Override
37-
public long getMaxLifetime() {
38-
return READ_WRITE.getMaxLifetime();
38+
public long connectionTimeout() {
39+
return READ_WRITE.connectionTimeout();
40+
}
41+
42+
@Override
43+
public long maxLifetime() {
44+
return READ_WRITE.maxLifetime();
3945
}
4046

4147
@Bean

src/main/java/br/com/multidatasources/config/datasource/ReplicaDataSourceConfiguration.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,33 @@
1414
public class ReplicaDataSourceConfiguration implements DataSourceConfiguration {
1515

1616
@Override
17-
public String getPoolName() {
18-
return READ_ONLY.getPoolName();
17+
public String poolName() {
18+
return READ_ONLY.poolName();
1919
}
2020

2121
@Override
22-
public int getMinimumIdle() {
23-
return READ_ONLY.getMinimumIdle();
22+
public int minimumIdle() {
23+
return READ_ONLY.minimumIdle();
2424
}
2525

2626
@Override
27-
public int getMaximumPoolSize() {
28-
return READ_ONLY.getMaximumPoolSize();
27+
public int maximumPoolSize() {
28+
return READ_ONLY.maximumPoolSize();
2929
}
3030

3131
@Override
32-
public long getConnectionTimeout() {
33-
return READ_ONLY.getConnectionTimeout();
32+
public long connectionTimeout() {
33+
return READ_ONLY.connectionTimeout();
3434
}
3535

3636
@Override
37-
public long getMaxLifetime() {
38-
return READ_ONLY.getMaxLifetime();
37+
public long idleTimeout() {
38+
return READ_ONLY.idleTimeout();
39+
}
40+
41+
@Override
42+
public long maxLifetime() {
43+
return READ_ONLY.maxLifetime();
3944
}
4045

4146
@Bean

src/main/java/br/com/multidatasources/config/routing/TransactionRoutingDataSource.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package br.com.multidatasources.config.routing;
22

3+
import io.opentelemetry.api.trace.Span;
34
import org.slf4j.Logger;
45
import org.slf4j.LoggerFactory;
56
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
@@ -15,12 +16,23 @@ public class TransactionRoutingDataSource extends AbstractRoutingDataSource {
1516
@Override
1617
protected Object determineCurrentLookupKey() {
1718
if (TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {
19+
1820
LOGGER.info("Routed to: {}", READ_ONLY);
21+
enrichSpan(READ_ONLY.name(), READ_ONLY.poolName());
22+
1923
return READ_ONLY;
2024
}
2125

2226
LOGGER.info("Routed to: {}", READ_WRITE);
27+
enrichSpan(READ_WRITE.name(), READ_WRITE.poolName());
28+
2329
return READ_WRITE;
2430
}
2531

32+
private void enrichSpan(final String dbType, final String pollName) {
33+
final var currentSpan = Span.current();
34+
currentSpan.setAttribute("db.type", dbType);
35+
currentSpan.setAttribute("db.poll", pollName);
36+
}
37+
2638
}

0 commit comments

Comments
 (0)