2 * Copyright 2017-2020 ZTE Corporation.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
16 package org.onap.holmes.common.utils;
\r
18 import io.dropwizard.db.DataSourceFactory;
\r
19 import io.dropwizard.jdbi.DBIFactory;
\r
20 import io.dropwizard.setup.Environment;
\r
21 import org.jvnet.hk2.annotations.Service;
\r
22 import org.skife.jdbi.v2.DBI;
\r
23 import org.skife.jdbi.v2.Handle;
\r
24 import org.slf4j.Logger;
\r
25 import org.slf4j.LoggerFactory;
\r
27 import javax.annotation.PostConstruct;
\r
28 import javax.inject.Inject;
\r
29 import javax.inject.Singleton;
\r
33 public class DbDaoUtil {
\r
35 private Logger log = LoggerFactory.getLogger(DbDaoUtil.class);
\r
39 private Environment environmentProvider;
\r
41 private DataSourceFactory dataSourceFactoryProvider;
\r
43 private DBIFactory factory = new DBIFactory();
\r
46 public synchronized void init() {
\r
48 jdbi = factory.build(environmentProvider, dataSourceFactoryProvider, "postgres");
\r
52 public <K> K getDao(Class<K> clazz) {
\r
54 return jdbi.open(clazz);
\r
55 } catch (Exception e) {
\r
56 log.warn("get object instance of Dao error.", e);
\r
61 public Handle getHandle() {
\r
64 } catch (Exception e) {
\r
65 log.warn("get object instance of Dao error.", e);
\r
70 public void close(Object obj) {
\r
74 } catch (Exception e) {
\r
75 log.warn("close jdbi connection error.", e);
\r
80 public <T> T getJdbiDaoByOnDemand(Class<T> daoClazz) {
\r
82 return jdbi.onDemand(daoClazz);
\r
86 public <T> T getJdbiDaoByOpen(Class<T> daoClazz) {
\r
88 return jdbi.open(daoClazz);
\r