/** * Copyright 2017-2020 ZTE Corporation. *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *

* http://www.apache.org/licenses/LICENSE-2.0 *

* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.onap.holmes.common.utils; import io.dropwizard.db.DataSourceFactory; import io.dropwizard.jdbi.DBIFactory; import io.dropwizard.setup.Environment; import org.jvnet.hk2.annotations.Service; import org.skife.jdbi.v2.DBI; import org.skife.jdbi.v2.Handle; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.annotation.PostConstruct; import javax.inject.Inject; import javax.inject.Singleton; @Singleton @Service public class DbDaoUtil { private Logger log = LoggerFactory.getLogger(DbDaoUtil.class); private DBI jdbi; @Inject private Environment environmentProvider; @Inject private DataSourceFactory dataSourceFactoryProvider; private DBIFactory factory = new DBIFactory(); @PostConstruct public synchronized void init() { if (jdbi == null) { jdbi = factory.build(environmentProvider, dataSourceFactoryProvider, "postgres"); } } public K getDao(Class clazz) { try { return jdbi.open(clazz); } catch (Exception e) { log.warn("get object instance of Dao error.", e); } return null; } public Handle getHandle() { try { return jdbi.open(); } catch (Exception e) { log.warn("get object instance of Dao error.", e); } return null; } public void close(Object obj) { if (obj != null) { try { jdbi.close(obj); } catch (Exception e) { log.warn("close jdbi connection error.", e); } } } public T getJdbiDaoByOnDemand(Class daoClazz) { return jdbi.onDemand(daoClazz); } public T getJdbiDaoByOpen(Class daoClazz) { return jdbi.open(daoClazz); } }