Aharoni, Pavel (pa0916) | ca3cb01 | 2018-10-22 15:29:57 +0300 | [diff] [blame] | 1 | /*-
|
| 2 | * ============LICENSE_START=======================================================
|
| 3 | * OSAM
|
| 4 | * ================================================================================
|
| 5 | * Copyright (C) 2018 AT&T
|
| 6 | * ================================================================================
|
| 7 | * Licensed under the Apache License, Version 2.0 (the "License");
|
| 8 | * you may not use this file except in compliance with the License.
|
| 9 | * You may obtain a copy of the License at
|
| 10 | *
|
| 11 | * http://www.apache.org/licenses/LICENSE-2.0
|
| 12 | *
|
| 13 | * Unless required by applicable law or agreed to in writing, software
|
| 14 | * distributed under the License is distributed on an "AS IS" BASIS,
|
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 16 | * See the License for the specific language governing permissions and
|
| 17 | * limitations under the License.
|
| 18 | * ============LICENSE_END=========================================================
|
| 19 | */
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 | package org.onap.osam.utils;
|
| 24 |
|
| 25 | import org.hibernate.Session;
|
| 26 | import org.hibernate.SessionFactory;
|
| 27 | import org.hibernate.Transaction;
|
| 28 | import org.onap.osam.exceptions.GenericUncheckedException;
|
| 29 | import org.onap.portalsdk.core.domain.FusionObject;
|
| 30 |
|
| 31 | import java.util.HashMap;
|
| 32 | import java.util.function.Function;
|
| 33 |
|
| 34 | public class DaoUtils {
|
| 35 |
|
| 36 | //all credit for this wonderful method go to is9613
|
| 37 | public static<T> T tryWithSessionAndTransaction(SessionFactory sessionFactory, Function<Session, T> update) {
|
| 38 | // opens a session and transactions, executes the input query.
|
| 39 | // gracefully close session and transaction once error occurres.
|
| 40 | Session session = null;
|
| 41 | Transaction tx = null;
|
| 42 | try {
|
| 43 | session = sessionFactory.openSession();
|
| 44 | tx = session.beginTransaction();
|
| 45 |
|
| 46 | T res = update.apply(session);
|
| 47 |
|
| 48 | tx.commit();
|
| 49 |
|
| 50 | return res;
|
| 51 | } catch (RuntimeException e) {
|
| 52 | try {
|
| 53 | if (tx != null) {
|
| 54 | tx.rollback();
|
| 55 | }
|
| 56 | } catch (RuntimeException e2) {
|
| 57 | // e2 is ingnored; we would like to know the
|
| 58 | // original failure reason, not only the reason
|
| 59 | // for rollback's failure
|
| 60 | throw new GenericUncheckedException("Failed rolling back transaction", e);
|
| 61 | }
|
| 62 | throw new GenericUncheckedException("Rolled back transaction", e);
|
| 63 | } finally {
|
| 64 | if (session != null) {
|
| 65 | session.close();
|
| 66 | }
|
| 67 | }
|
| 68 | }
|
| 69 |
|
| 70 | public static HashMap<String, Object> getPropsMap() {
|
| 71 | HashMap<String, Object> props = new HashMap<>();
|
| 72 | props.put(FusionObject.Parameters.PARAM_USERID, 0);
|
| 73 | return props;
|
| 74 | }
|
| 75 | }
|