Skip to content

Commit fe80808

Browse files
committed
jOOQ dao refactor, use explicit transactions not thread locals
1 parent 1799fe9 commit fe80808

File tree

3 files changed

+39
-131
lines changed

3 files changed

+39
-131
lines changed

stubbornjava-common/src/main/java/com/stubbornjava/common/db/Dao.java

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.stubbornjava.common.db;
22

3-
import static org.jooq.impl.DSL.using;
4-
53
import java.util.ArrayList;
64
import java.util.Arrays;
75
import java.util.Collection;
@@ -10,7 +8,7 @@
108
import java.util.function.Function;
119

1210
import org.jooq.Condition;
13-
import org.jooq.Configuration;
11+
import org.jooq.DSLContext;
1412
import org.jooq.Field;
1513
import org.jooq.Record;
1614
import org.jooq.RecordMapper;
@@ -19,105 +17,102 @@
1917
import org.jooq.UpdatableRecord;
2018
import org.jooq.impl.TableImpl;
2119

22-
public class Dao<Rec extends UpdatableRecord<Rec>, T> {
23-
private final TableImpl<Rec> table;
20+
public class Dao<Rec extends UpdatableRecord<Rec>, T, Table extends TableImpl<Rec>> {
21+
private final Table table;
2422
private final RecordMapper<Rec, T> mapper;
2523
private final RecordUnmapper<T, Rec> unmapper;
26-
private final Configuration configuration;
27-
public Dao(TableImpl<Rec> table,
28-
RecordMapper<Rec, T> mapper,
29-
RecordUnmapper<T, Rec> unmapper,
30-
Configuration configuration) {
24+
public Dao(Table table,
25+
RecordMapper<Rec, T> mapper,
26+
RecordUnmapper<T, Rec> unmapper) {
3127
super();
3228
this.table = table;
3329
this.mapper = mapper;
3430
this.unmapper = unmapper;
35-
this.configuration = configuration;
3631
}
3732

38-
public T insertReturning(T obj) {
39-
Rec rec = records(Collections.singletonList(obj), false).get(0);
33+
public T insertReturning(DSLContext ctx, T obj) {
34+
Rec rec = records(ctx, Collections.singletonList(obj), false).get(0);
4035
rec.insert();
4136
return mapper.map(rec);
4237
}
4338

44-
public void insert(T obj) {
45-
insert(Collections.singletonList(obj));
39+
public void insert(DSLContext ctx, T obj) {
40+
insert(ctx, Collections.singletonList(obj));
4641
}
4742

4843
@SuppressWarnings("unchecked")
49-
public void insert(T... objects) {
50-
insert(Arrays.asList(objects));
44+
public void insert(DSLContext ctx, T... objects) {
45+
insert(ctx, Arrays.asList(objects));
5146
}
5247

53-
public void insert(Collection<T> objects) {
48+
public void insert(DSLContext ctx, Collection<T> objects) {
5449
// Execute a batch INSERT
5550
if (objects.size() > 1) {
56-
using(configuration).batchInsert(records(objects, false)).execute();
51+
ctx.batchInsert(records(ctx, objects, false)).execute();
5752
}
5853

5954
// Execute a regular INSERT
6055
else if (objects.size() == 1) {
61-
records(objects, false).get(0).insert();
56+
records(ctx, objects, false).get(0).insert();
6257
}
6358
}
6459

65-
public void update(T obj) {
66-
update(Collections.singletonList(obj));
60+
public void update(DSLContext ctx, T obj) {
61+
update(ctx, Collections.singletonList(obj));
6762
}
6863

6964
@SuppressWarnings("unchecked")
70-
public void update(T... objects) {
71-
update(Arrays.asList(objects));
65+
public void update(DSLContext ctx, T... objects) {
66+
update(ctx, Arrays.asList(objects));
7267
}
7368

74-
public void update(Collection<T> objects) {
69+
public void update(DSLContext ctx, Collection<T> objects) {
7570
// Execute a batch UPDATE
7671
if (objects.size() > 1) {
77-
using(configuration).batchUpdate(records(objects, false)).execute();
72+
ctx.batchUpdate(records(ctx, objects, false)).execute();
7873
}
7974

8075
// Execute a regular UPDATE
8176
else if (objects.size() == 1) {
82-
records(objects, false).get(0).update();
77+
records(ctx, objects, false).get(0).update();
8378
}
8479
}
8580

86-
public void delete(T obj) {
87-
delete(Collections.singletonList(obj));
81+
public void delete(DSLContext ctx, T obj) {
82+
delete(ctx, Collections.singletonList(obj));
8883
}
8984

9085
@SuppressWarnings("unchecked")
91-
public void delete(T... objects) {
92-
delete(Arrays.asList(objects));
86+
public void delete(DSLContext ctx, T... objects) {
87+
delete(ctx, Arrays.asList(objects));
9388
}
9489

95-
public void delete(Collection<T> objects) {
90+
public void delete(DSLContext ctx, Collection<T> objects) {
9691
// Execute a batch DELETE
9792
if (objects.size() > 1) {
98-
using(configuration).batchDelete(records(objects, false)).execute();
93+
ctx.batchDelete(records(ctx, objects, false)).execute();
9994
}
10095

10196
// Execute a regular DELETE
10297
else if (objects.size() == 1) {
103-
records(objects, false).get(0).delete();
98+
records(ctx, objects, false).get(0).delete();
10499
}
105100
}
106101

107-
public T fetchOne(Function<TableImpl<Rec>, Condition> func) {
108-
return mapper.map(using(configuration).fetchOne(table, func.apply(table)));
102+
public T fetchOne(DSLContext ctx, Function<Table, Condition> func) {
103+
return mapper.map(ctx.fetchOne(table, func.apply(table)));
109104
}
110105

111-
public List<T> fetch(Function<TableImpl<Rec>, Condition> func) {
112-
return using(configuration).fetch(table, func.apply(table)).map(mapper);
106+
public List<T> fetch(DSLContext ctx, Function<Table, Condition> func) {
107+
return ctx.fetch(table, func.apply(table)).map(mapper);
113108
}
114109

115-
public List<T> fetchAll() {
116-
return using(configuration).fetch(table).map(mapper);
110+
public List<T> fetchAll(DSLContext ctx) {
111+
return ctx.fetch(table).map(mapper);
117112
}
118113

119-
public int deleteWhere(Function<TableImpl<Rec>, Condition> func) {
120-
return using(configuration).deleteFrom(table).where(func.apply(table)).execute();
114+
public int deleteWhere(DSLContext ctx, Function<TableImpl<Rec>, Condition> func) {
115+
return ctx.deleteFrom(table).where(func.apply(table)).execute();
121116
}
122117

123118
// Copy pasted from jOOQ's DAOImpl.java
@@ -127,13 +122,13 @@ public int deleteWhere(Function<TableImpl<Rec>, Condition> func) {
127122
}
128123

129124
// Copy pasted from jOOQ's DAOImpl.java
130-
private /* non-final */ List<Rec> records(Collection<T> objects, boolean forUpdate) {
125+
private /* non-final */ List<Rec> records(DSLContext ctx, Collection<T> objects, boolean forUpdate) {
131126
List<Rec> result = new ArrayList<>();
132127
Field<?>[] pk = pk();
133128

134129
for (T object : objects) {
135130
Rec record = unmapper.unmap(object);
136-
record.attach(using(configuration).configuration());
131+
record.attach(ctx.configuration());
137132

138133
if (forUpdate && pk != null)
139134
for (Field<?> field : pk)

stubbornjava-common/src/main/java/com/stubbornjava/common/db/NamedConfiguration.java

Lines changed: 0 additions & 22 deletions
This file was deleted.

stubbornjava-common/src/main/java/com/stubbornjava/common/db/ThreadLocalJooqConfig.java

Lines changed: 0 additions & 65 deletions
This file was deleted.

0 commit comments

Comments
 (0)