Skip to content

Commit a46c721

Browse files
committed
more unit tests for hbm
1 parent 47674e1 commit a46c721

File tree

6 files changed

+774
-43
lines changed

6 files changed

+774
-43
lines changed

jooby-hbm/src/main/java/org/jooby/hbm/Hbm.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,17 +201,17 @@ public void configure(final Env mode, final Config config, final Binder binder)
201201
emf = new HbmProvider(descriptor, config(config, classes));
202202
keys(EntityManagerFactory.class, key -> binder.bind(key).toProvider(emf).asEagerSingleton());
203203

204-
Multibinder<Route.Definition> routes = Multibinder.newSetBinder(binder, Route.Definition.class);
205-
206204
List<Key<EntityManager>> emkeys = new ArrayList<>();
207205

208206
keys(EntityManager.class, key -> {
209-
binder.bind(key).toProvider(() -> {
207+
Provider<EntityManager> em = () -> {
210208
throw new OutOfScopeException("Cannot access " + key + " outside of a scoping block");
211-
}).in(RequestScoped.class);
209+
};
210+
binder.bind(key).toProvider(em).in(RequestScoped.class);
212211
emkeys.add(key);
213212
});
214213

214+
Multibinder<Route.Definition> routes = Multibinder.newSetBinder(binder, Route.Definition.class);
215215
routes.addBinding().toInstance(
216216
new Route.Definition("*", "*", new OpenSessionInView(emf, emkeys)).name("hbm")
217217
);

jooby-hbm/src/main/java/org/jooby/hbm/OpenSessionInView.java

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
119
package org.jooby.hbm;
220

321
import java.util.List;
422

23+
import javax.inject.Provider;
524
import javax.persistence.EntityManager;
625
import javax.persistence.EntityTransaction;
726

@@ -14,7 +33,6 @@
1433
import org.jooby.Response;
1534
import org.jooby.Route;
1635
import org.jooby.Route.Chain;
17-
import org.jooby.internal.hbm.HbmProvider;
1836
import org.jooby.internal.hbm.TrxResponse;
1937
import org.slf4j.Logger;
2038
import org.slf4j.LoggerFactory;
@@ -26,11 +44,12 @@ public class OpenSessionInView implements Route.Filter {
2644
/** The logging system. */
2745
private final Logger log = LoggerFactory.getLogger(getClass());
2846

29-
private HbmProvider emf;
47+
private Provider<HibernateEntityManagerFactory> emf;
3048

3149
private List<Key<EntityManager>> keys;
3250

33-
public OpenSessionInView(final HbmProvider emf, final List<Key<EntityManager>> keys) {
51+
public OpenSessionInView(final Provider<HibernateEntityManagerFactory> emf,
52+
final List<Key<EntityManager>> keys) {
3453
this.emf = emf;
3554
this.keys = keys;
3655
}
@@ -43,39 +62,37 @@ public void handle(final Request req, final Response rsp, final Chain chain) thr
4362
EntityManager em = hemf.createEntityManager();
4463
Session session = (Session) em.getDelegate();
4564
String sessionId = Integer.toHexString(System.identityHashCode(session));
65+
keys.forEach(key -> req.set(key, em));
4666

4767
log.debug("session opened: {}", sessionId);
48-
log.debug(" [{}] binding", sessionId);
49-
50-
ManagedSessionContext.bind(session);
51-
52-
keys.forEach(key -> req.set(key, em));
53-
FlushMode flushMode = FlushMode.AUTO;
54-
log.debug(" [{}] flush mode: {}", sessionId, flushMode);
55-
session.setFlushMode(flushMode);
5668
EntityTransaction trx = em.getTransaction();
5769
try {
70+
log.debug(" [{}] binding", sessionId);
71+
ManagedSessionContext.bind(session);
72+
73+
FlushMode flushMode = FlushMode.AUTO;
74+
log.debug(" [{}] flush mode: {}", sessionId, flushMode);
75+
session.setFlushMode(flushMode);
76+
5877
log.debug(" [{}] starting transation: {}", sessionId, trx);
5978
trx.begin();
6079

6180
// invoke next handler
6281
chain.next(req, new TrxResponse(rsp, em));
6382
} finally {
64-
try {
65-
if (trx.isActive()) {
66-
log.debug(" [{}] rolling back transation: {}", sessionId, trx);
67-
trx.rollback();
68-
}
69-
} finally {
70-
try {
71-
log.debug(" [{}] closing", sessionId);
72-
em.close();
73-
} finally {
74-
log.debug(" [{}] unbinding", sessionId);
75-
ManagedSessionContext.unbind(sf);
76-
log.debug("session released: [{}]", sessionId);
77-
}
78-
}
83+
closeUnbind(sf, em, sessionId);
84+
}
85+
}
86+
87+
private void closeUnbind(final SessionFactory sf, final EntityManager em,
88+
final String sessionId) {
89+
try {
90+
log.debug(" [{}] closing", sessionId);
91+
em.close();
92+
} finally {
93+
log.debug(" [{}] unbinding", sessionId);
94+
ManagedSessionContext.unbind(sf);
95+
log.debug("session released: [{}]", sessionId);
7996
}
8097
}
8198

jooby-hbm/src/main/java/org/jooby/internal/hbm/TrxResponse.java

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
119
package org.jooby.internal.hbm;
220

321
import java.sql.Connection;
@@ -38,7 +56,7 @@ private void trxSend(final Object result) throws Exception {
3856
} catch (Exception ex) {
3957
log.trace(" [" + sessionId + "] unable to setReadOnly " + readOnly, ex);
4058
}
41-
session.setDefaultReadOnly(true);
59+
session.setDefaultReadOnly(readOnly);
4260
};
4361

4462
EntityTransaction trx = em.getTransaction();
@@ -51,30 +69,37 @@ private void trxSend(final Object result) throws Exception {
5169
trx.commit();
5270
}
5371

54-
// start new transaction
55-
log.debug(" [{}] setting connection to read only", sessionId);
56-
setReadOnly.accept(true);
57-
session.setFlushMode(FlushMode.MANUAL);
58-
EntityTransaction readOnlyTrx = em.getTransaction();
59-
log.debug(" [{}] starting readonly transaction: {}", sessionId, readOnlyTrx);
60-
readOnlyTrx.begin();
61-
72+
EntityTransaction readOnlyTrx = null;
6273
try {
74+
// start new transaction
75+
log.debug(" [{}] setting connection to read only", sessionId);
76+
setReadOnly.accept(true);
77+
session.setFlushMode(FlushMode.MANUAL);
78+
readOnlyTrx = em.getTransaction();
79+
log.debug(" [{}] starting readonly transaction: {}", sessionId, readOnlyTrx);
80+
readOnlyTrx.begin();
81+
6382
// send it!
6483
super.send(result);
6584

6685
log.debug(" [{}] commiting readonly transaction: {}", sessionId, readOnlyTrx);
6786
readOnlyTrx.commit();
6887
} catch (Exception ex) {
69-
if (readOnlyTrx.isActive()) {
88+
if (readOnlyTrx != null && readOnlyTrx.isActive()) {
7089
log.debug(" [{}] rolling back readonly transaction: {}", sessionId, readOnlyTrx);
7190
readOnlyTrx.rollback();
7291
}
7392
throw ex;
93+
} finally {
94+
log.debug(" [{}] removing readonly mode from connection", sessionId);
95+
setReadOnly.accept(false);
96+
}
97+
} catch (Exception ex) {
98+
if (trx.isActive()) {
99+
log.debug(" [{}] rolling back transation: {}", sessionId, trx);
100+
trx.rollback();
74101
}
75-
} finally {
76-
log.debug(" [{}] removing readonly mode from connection", sessionId);
77-
setReadOnly.accept(false);
102+
throw ex;
78103
}
79104
}
80105

0 commit comments

Comments
 (0)