Skip to content

Commit f4786af

Browse files
committed
define DSLContext as a bean and switch to using it
instead of calling "DSL.using(config)" to create a DSLContext for every db operation, init DSLContext only once as a Spring bean and inject it instead of the config.
1 parent 7c13793 commit f4786af

9 files changed

Lines changed: 91 additions & 143 deletions

src/main/java/de/rwth/idsg/steve/config/BeanConfiguration.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
import com.zaxxer.hikari.HikariDataSource;
99
import de.rwth.idsg.steve.SteveConfiguration;
1010
import lombok.extern.slf4j.Slf4j;
11+
import org.jooq.DSLContext;
1112
import org.jooq.SQLDialect;
1213
import org.jooq.conf.Settings;
14+
import org.jooq.impl.DSL;
1315
import org.jooq.impl.DataSourceConnectionProvider;
1416
import org.jooq.impl.DefaultConfiguration;
1517
import org.springframework.beans.factory.annotation.Qualifier;
@@ -87,6 +89,23 @@ public org.jooq.Configuration jooqConfig() {
8789
.set(new Settings().withExecuteLogging(SteveConfiguration.DB.SQL_LOGGING));
8890
}
8991

92+
/**
93+
* Can we re-use DSLContext as a Spring bean (singleton)? Yes, the Spring tutorial of
94+
* Jooq also does it that way, but only if we do not change anything about the
95+
* config after the init (which we don't do anyways) and if the ConnectionProvider
96+
* does not store any shared state (we use DataSourceConnectionProvider of Jooq, so no problem).
97+
*
98+
* Some sources and discussion:
99+
* - http://www.jooq.org/doc/3.6/manual/getting-started/tutorials/jooq-with-spring/
100+
* - http://jooq-user.narkive.com/2fvuLodn/dslcontext-and-threads
101+
* - https://groups.google.com/forum/#!topic/jooq-user/VK7KQcjj3Co
102+
* - http://stackoverflow.com/questions/32848865/jooq-dslcontext-correct-autowiring-with-spring
103+
*/
104+
@Bean
105+
public DSLContext dslContext() {
106+
return DSL.using(jooqConfig());
107+
}
108+
90109
@Bean
91110
public ScheduledExecutorService scheduledExecutorService() {
92111
ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("SteVe-Executor-%d")

src/main/java/de/rwth/idsg/steve/repository/impl/ChargePointRepositoryImpl.java

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import jooq.steve.db.tables.records.ChargeBoxRecord;
1616
import lombok.extern.slf4j.Slf4j;
1717
import org.joda.time.DateTime;
18-
import org.jooq.Configuration;
1918
import org.jooq.DSLContext;
2019
import org.jooq.Field;
2120
import org.jooq.Record1;
@@ -27,7 +26,6 @@
2726
import org.jooq.exception.DataAccessException;
2827
import org.jooq.impl.DSL;
2928
import org.springframework.beans.factory.annotation.Autowired;
30-
import org.springframework.beans.factory.annotation.Qualifier;
3129
import org.springframework.stereotype.Repository;
3230

3331
import java.util.List;
@@ -47,16 +45,12 @@
4745
@Repository
4846
public class ChargePointRepositoryImpl implements ChargePointRepository {
4947

50-
@Autowired
51-
@Qualifier("jooqConfig")
52-
private Configuration config;
53-
48+
@Autowired private DSLContext ctx;
5449
@Autowired private AddressRepository addressRepository;
5550

5651
@Override
5752
public boolean isRegistered(String chargeBoxId) {
58-
Record1<Integer> r = DSL.using(config)
59-
.selectOne()
53+
Record1<Integer> r = ctx.selectOne()
6054
.from(CHARGE_BOX)
6155
.where(CHARGE_BOX.CHARGE_BOX_ID.eq(chargeBoxId))
6256
.fetchOne();
@@ -68,8 +62,7 @@ public boolean isRegistered(String chargeBoxId) {
6862
public List<ChargePointSelect> getChargePointSelect(OcppProtocol protocol) {
6963
final OcppTransport transport = protocol.getTransport();
7064

71-
return DSL.using(config)
72-
.select(CHARGE_BOX.CHARGE_BOX_ID, CHARGE_BOX.ENDPOINT_ADDRESS)
65+
return ctx.select(CHARGE_BOX.CHARGE_BOX_ID, CHARGE_BOX.ENDPOINT_ADDRESS)
7366
.from(CHARGE_BOX)
7467
.where(CHARGE_BOX.OCPP_PROTOCOL.equal(protocol.getCompositeValue()))
7568
.and(CHARGE_BOX.ENDPOINT_ADDRESS.isNotNull())
@@ -79,16 +72,14 @@ public List<ChargePointSelect> getChargePointSelect(OcppProtocol protocol) {
7972

8073
@Override
8174
public List<String> getChargeBoxIds() {
82-
return DSL.using(config)
83-
.select(CHARGE_BOX.CHARGE_BOX_ID)
75+
return ctx.select(CHARGE_BOX.CHARGE_BOX_ID)
8476
.from(CHARGE_BOX)
8577
.fetch(CHARGE_BOX.CHARGE_BOX_ID);
8678
}
8779

8880
@Override
8981
public Map<String, Integer> getChargeBoxIdPkPair(List<String> chargeBoxIdList) {
90-
return DSL.using(config)
91-
.select(CHARGE_BOX.CHARGE_BOX_ID, CHARGE_BOX.CHARGE_BOX_PK)
82+
return ctx.select(CHARGE_BOX.CHARGE_BOX_ID, CHARGE_BOX.CHARGE_BOX_PK)
9283
.from(CHARGE_BOX)
9384
.where(CHARGE_BOX.CHARGE_BOX_ID.in(chargeBoxIdList))
9485
.fetchMap(CHARGE_BOX.CHARGE_BOX_ID, CHARGE_BOX.CHARGE_BOX_PK);
@@ -109,7 +100,7 @@ public List<ChargePoint.Overview> getOverview(ChargePointQueryForm form) {
109100

110101
@SuppressWarnings("unchecked")
111102
private Result<Record5<Integer, String, String, String, DateTime>> getOverviewInternal(ChargePointQueryForm form) {
112-
SelectQuery selectQuery = DSL.using(config).selectQuery();
103+
SelectQuery selectQuery = ctx.selectQuery();
113104
selectQuery.addFrom(CHARGE_BOX);
114105
selectQuery.addSelect(
115106
CHARGE_BOX.CHARGE_BOX_PK,
@@ -163,8 +154,6 @@ private Result<Record5<Integer, String, String, String, DateTime>> getOverviewIn
163154

164155
@Override
165156
public ChargePoint.Details getDetails(int chargeBoxPk) {
166-
DSLContext ctx = DSL.using(config);
167-
168157
ChargeBoxRecord cbr = ctx.selectFrom(CHARGE_BOX)
169158
.where(CHARGE_BOX.CHARGE_BOX_PK.equal(chargeBoxPk))
170159
.fetchOne();
@@ -185,13 +174,12 @@ public List<ConnectorStatus> getChargePointConnectorStatus() {
185174
// Prepare for the inner select of the second join
186175
Field<Integer> t1Pk = CONNECTOR_STATUS.CONNECTOR_PK.as("t1_pk");
187176
Field<DateTime> t1Max = DSL.max(CONNECTOR_STATUS.STATUS_TIMESTAMP).as("t1_max");
188-
TableLike<?> t1 = DSL.select(t1Pk, t1Max)
177+
TableLike<?> t1 = ctx.select(t1Pk, t1Max)
189178
.from(CONNECTOR_STATUS)
190179
.groupBy(CONNECTOR_STATUS.CONNECTOR_PK)
191180
.asTable("t1");
192181

193-
return DSL.using(config)
194-
.select(CHARGE_BOX.CHARGE_BOX_PK,
182+
return ctx.select(CHARGE_BOX.CHARGE_BOX_PK,
195183
CONNECTOR.CHARGE_BOX_ID,
196184
CONNECTOR.CONNECTOR_ID,
197185
CONNECTOR_STATUS.STATUS_TIMESTAMP,
@@ -220,16 +208,15 @@ public List<ConnectorStatus> getChargePointConnectorStatus() {
220208

221209
@Override
222210
public List<Integer> getConnectorIds(String chargeBoxId) {
223-
return DSL.using(config)
224-
.select(CONNECTOR.CONNECTOR_ID)
211+
return ctx.select(CONNECTOR.CONNECTOR_ID)
225212
.from(CONNECTOR)
226213
.where(CONNECTOR.CHARGE_BOX_ID.equal(chargeBoxId))
227214
.fetch(CONNECTOR.CONNECTOR_ID);
228215
}
229216

230217
@Override
231218
public void addChargePoint(ChargePointForm form) {
232-
DSL.using(config).transaction(configuration -> {
219+
ctx.transaction(configuration -> {
233220
DSLContext ctx = DSL.using(configuration);
234221
try {
235222
Integer addressId = addressRepository.updateOrInsert(ctx, form.getAddress());
@@ -244,7 +231,7 @@ public void addChargePoint(ChargePointForm form) {
244231

245232
@Override
246233
public void updateChargePoint(ChargePointForm form) {
247-
DSL.using(config).transaction(configuration -> {
234+
ctx.transaction(configuration -> {
248235
DSLContext ctx = DSL.using(configuration);
249236
try {
250237
Integer addressId = addressRepository.updateOrInsert(ctx, form.getAddress());
@@ -259,7 +246,7 @@ public void updateChargePoint(ChargePointForm form) {
259246

260247
@Override
261248
public void deleteChargePoint(int chargeBoxPk) {
262-
DSL.using(config).transaction(configuration -> {
249+
ctx.transaction(configuration -> {
263250
DSLContext ctx = DSL.using(configuration);
264251
try {
265252
addressRepository.delete(ctx, selectAddressId(chargeBoxPk));
@@ -276,7 +263,7 @@ public void deleteChargePoint(int chargeBoxPk) {
276263
// -------------------------------------------------------------------------
277264

278265
private SelectConditionStep<Record1<Integer>> selectAddressId(int chargeBoxPk) {
279-
return DSL.select(CHARGE_BOX.ADDRESS_PK)
266+
return ctx.select(CHARGE_BOX.ADDRESS_PK)
280267
.from(CHARGE_BOX)
281268
.where(CHARGE_BOX.CHARGE_BOX_PK.eq(chargeBoxPk));
282269
}

src/main/java/de/rwth/idsg/steve/repository/impl/GenericRepositoryImpl.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77
import jooq.steve.db.routines.GetStats;
88
import lombok.extern.slf4j.Slf4j;
99
import org.joda.time.DateTime;
10-
import org.jooq.Configuration;
10+
import org.jooq.DSLContext;
1111
import org.jooq.Record2;
12-
import org.jooq.impl.DSL;
1312
import org.springframework.beans.factory.annotation.Autowired;
14-
import org.springframework.beans.factory.annotation.Qualifier;
1513
import org.springframework.stereotype.Repository;
1614

1715
import static jooq.steve.db.tables.SchemaVersion.SCHEMA_VERSION;
@@ -26,16 +24,14 @@
2624
@Repository
2725
public class GenericRepositoryImpl implements GenericRepository {
2826

29-
@Autowired
30-
@Qualifier("jooqConfig")
31-
private Configuration config;
27+
@Autowired private DSLContext ctx;
3228

3329
@Override
3430
public Statistics getStats() {
3531

3632
// getStats is the stored procedure in our MySQL DB
3733
GetStats gs = new GetStats();
38-
gs.execute(config);
34+
gs.execute(ctx.configuration());
3935

4036
gs.detach();
4137

@@ -57,8 +53,7 @@ public Statistics getStats() {
5753

5854
@Override
5955
public DbVersion getDBVersion() {
60-
Record2<String, DateTime> record = DSL.using(config)
61-
.select(SCHEMA_VERSION.VERSION, SCHEMA_VERSION.INSTALLED_ON)
56+
Record2<String, DateTime> record = ctx.select(SCHEMA_VERSION.VERSION, SCHEMA_VERSION.INSTALLED_ON)
6257
.from(SCHEMA_VERSION)
6358
.where(SCHEMA_VERSION.VERSION_RANK.eq(
6459
select(max(SCHEMA_VERSION.VERSION_RANK)).from(SCHEMA_VERSION)))

src/main/java/de/rwth/idsg/steve/repository/impl/OcppServerRepositoryImpl.java

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@
1515
import ocpp.cs._2012._06.ValueFormat;
1616
import org.joda.time.DateTime;
1717
import org.jooq.BatchBindStep;
18-
import org.jooq.Configuration;
1918
import org.jooq.DSLContext;
2019
import org.jooq.impl.DSL;
2120
import org.springframework.beans.factory.annotation.Autowired;
22-
import org.springframework.beans.factory.annotation.Qualifier;
2321
import org.springframework.stereotype.Repository;
2422

2523
import java.util.List;
@@ -42,16 +40,12 @@
4240
@Repository
4341
public class OcppServerRepositoryImpl implements OcppServerRepository {
4442

45-
@Autowired
46-
@Qualifier("jooqConfig")
47-
private Configuration config;
48-
43+
@Autowired private DSLContext ctx;
4944
@Autowired private ReservationRepository reservationRepository;
5045

5146
@Override
5247
public boolean updateChargebox(UpdateChargeboxParams p) {
53-
int count = DSL.using(config)
54-
.update(CHARGE_BOX)
48+
int count = ctx.update(CHARGE_BOX)
5549
.set(CHARGE_BOX.OCPP_PROTOCOL, p.getOcppProtocol().getCompositeValue())
5650
.set(CHARGE_BOX.CHARGE_POINT_VENDOR, p.getVendor())
5751
.set(CHARGE_BOX.CHARGE_POINT_MODEL, p.getModel())
@@ -79,17 +73,15 @@ public boolean updateChargebox(UpdateChargeboxParams p) {
7973

8074
@Override
8175
public void updateEndpointAddress(String chargeBoxIdentity, String endpointAddress) {
82-
DSL.using(config)
83-
.update(CHARGE_BOX)
76+
ctx.update(CHARGE_BOX)
8477
.set(CHARGE_BOX.ENDPOINT_ADDRESS, endpointAddress)
8578
.where(CHARGE_BOX.CHARGE_BOX_ID.equal(chargeBoxIdentity))
8679
.execute();
8780
}
8881

8982
@Override
9083
public void updateChargeboxFirmwareStatus(String chargeBoxIdentity, String firmwareStatus) {
91-
DSL.using(config)
92-
.update(CHARGE_BOX)
84+
ctx.update(CHARGE_BOX)
9385
.set(CHARGE_BOX.FW_UPDATE_STATUS, firmwareStatus)
9486
.set(CHARGE_BOX.FW_UPDATE_TIMESTAMP, CustomDSL.utcTimestamp())
9587
.where(CHARGE_BOX.CHARGE_BOX_ID.equal(chargeBoxIdentity))
@@ -98,8 +90,7 @@ public void updateChargeboxFirmwareStatus(String chargeBoxIdentity, String firmw
9890

9991
@Override
10092
public void updateChargeboxDiagnosticsStatus(String chargeBoxIdentity, String status) {
101-
DSL.using(config)
102-
.update(CHARGE_BOX)
93+
ctx.update(CHARGE_BOX)
10394
.set(CHARGE_BOX.DIAGNOSTICS_STATUS, status)
10495
.set(CHARGE_BOX.DIAGNOSTICS_TIMESTAMP, CustomDSL.utcTimestamp())
10596
.where(CHARGE_BOX.CHARGE_BOX_ID.equal(chargeBoxIdentity))
@@ -108,8 +99,7 @@ public void updateChargeboxDiagnosticsStatus(String chargeBoxIdentity, String st
10899

109100
@Override
110101
public void updateChargeboxHeartbeat(String chargeBoxIdentity, DateTime ts) {
111-
DSL.using(config)
112-
.update(CHARGE_BOX)
102+
ctx.update(CHARGE_BOX)
113103
.set(CHARGE_BOX.LAST_HEARTBEAT_TIMESTAMP, ts)
114104
.where(CHARGE_BOX.CHARGE_BOX_ID.equal(chargeBoxIdentity))
115105
.execute();
@@ -118,7 +108,7 @@ public void updateChargeboxHeartbeat(String chargeBoxIdentity, DateTime ts) {
118108
@Override
119109
public void insertConnectorStatus(InsertConnectorStatusParams p) {
120110

121-
DSL.using(config).transaction(configuration -> {
111+
ctx.transaction(configuration -> {
122112
DSLContext ctx = DSL.using(configuration);
123113

124114
// Step 1
@@ -150,7 +140,7 @@ public void insertConnectorStatus(InsertConnectorStatusParams p) {
150140
public void insertMeterValues12(final String chargeBoxIdentity, final int connectorId,
151141
final List<ocpp.cs._2010._08.MeterValue> list) {
152142

153-
DSL.using(config).transaction(configuration -> {
143+
ctx.transaction(configuration -> {
154144
DSLContext ctx = DSL.using(configuration);
155145

156146
insertIgnoreConnector(ctx, chargeBoxIdentity, connectorId);
@@ -163,7 +153,7 @@ public void insertMeterValues12(final String chargeBoxIdentity, final int connec
163153
public void insertMeterValues15(final String chargeBoxIdentity, final int connectorId,
164154
final List<ocpp.cs._2012._06.MeterValue> list, final Integer transactionId) {
165155

166-
DSL.using(config).transaction(configuration -> {
156+
ctx.transaction(configuration -> {
167157
DSLContext ctx = DSL.using(configuration);
168158

169159
insertIgnoreConnector(ctx, chargeBoxIdentity, connectorId);
@@ -176,7 +166,7 @@ public void insertMeterValues15(final String chargeBoxIdentity, final int connec
176166
public void insertMeterValuesOfTransaction(String chargeBoxIdentity, final int transactionId,
177167
final List<MeterValue> list) {
178168

179-
DSL.using(config).transaction(configuration -> {
169+
ctx.transaction(configuration -> {
180170
DSLContext ctx = DSL.using(configuration);
181171

182172
// First, get connector primary key from transaction table
@@ -193,7 +183,7 @@ public void insertMeterValuesOfTransaction(String chargeBoxIdentity, final int t
193183
@Override
194184
public Integer insertTransaction(InsertTransactionParams p) {
195185

196-
return DSL.using(config).transactionResult(configuration -> {
186+
return ctx.transactionResult(configuration -> {
197187
DSLContext ctx = DSL.using(configuration);
198188

199189
insertIgnoreConnector(ctx, p.getChargeBoxId(), p.getConnectorId());
@@ -233,8 +223,7 @@ public Integer insertTransaction(InsertTransactionParams p) {
233223
*/
234224
@Override
235225
public void updateTransaction(int transactionId, DateTime stopTimestamp, String stopMeterValue) {
236-
DSL.using(config)
237-
.update(TRANSACTION)
226+
ctx.update(TRANSACTION)
238227
.set(TRANSACTION.STOP_TIMESTAMP, stopTimestamp)
239228
.set(TRANSACTION.STOP_VALUE, stopMeterValue)
240229
.where(TRANSACTION.TRANSACTION_PK.equal(transactionId))

0 commit comments

Comments
 (0)