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+ */
119package org .jooby .hbm ;
220
321import java .util .List ;
422
23+ import javax .inject .Provider ;
524import javax .persistence .EntityManager ;
625import javax .persistence .EntityTransaction ;
726
1433import org .jooby .Response ;
1534import org .jooby .Route ;
1635import org .jooby .Route .Chain ;
17- import org .jooby .internal .hbm .HbmProvider ;
1836import org .jooby .internal .hbm .TrxResponse ;
1937import org .slf4j .Logger ;
2038import 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
0 commit comments