2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.so.etsi.nfvo.ns.lcm.database.config;
22 import javax.persistence.EntityManagerFactory;
23 import javax.sql.DataSource;
24 import org.onap.so.etsi.nfvo.ns.lcm.database.beans.NfvoJob;
25 import org.springframework.beans.factory.annotation.Autowired;
26 import org.springframework.beans.factory.annotation.Qualifier;
27 import org.springframework.boot.context.properties.ConfigurationProperties;
28 import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
29 import org.springframework.context.annotation.Bean;
30 import org.springframework.context.annotation.Configuration;
31 import org.springframework.context.annotation.Primary;
32 import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
33 import org.springframework.jmx.export.MBeanExporter;
34 import org.springframework.orm.jpa.JpaTransactionManager;
35 import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
36 import org.springframework.transaction.PlatformTransactionManager;
37 import org.springframework.transaction.annotation.EnableTransactionManagement;
38 import com.zaxxer.hikari.HikariConfig;
39 import com.zaxxer.hikari.HikariDataSource;
42 * @author Waqas Ikram (waqas.ikram@est.tech)
46 @EnableTransactionManagement
47 @EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactory",
48 basePackages = {"org.onap.so.etsi.nfvo.ns.lcm.database.repository"})
49 public class NfvoDatabaseConfiguration {
51 private static final String PERSISTENCE_UNIT = "nfvo";
52 private static final String NFVO_DATA_SOURCE_QUALIFIER = "nfvoDataSource";
54 @Autowired(required = false)
55 private MBeanExporter mBeanExporter;
58 @ConfigurationProperties(prefix = "spring.datasource.hikari.nfvo")
59 public HikariConfig nfvoDbConfig() {
60 return new HikariConfig();
63 @Bean(name = NFVO_DATA_SOURCE_QUALIFIER)
64 public DataSource dataSource() {
65 if (mBeanExporter != null) {
66 mBeanExporter.addExcludedBean(NFVO_DATA_SOURCE_QUALIFIER);
68 final HikariConfig hikariConfig = this.nfvoDbConfig();
69 return new HikariDataSource(hikariConfig);
73 @Bean(name = "entityManagerFactory")
74 public LocalContainerEntityManagerFactoryBean entityManagerFactory(final EntityManagerFactoryBuilder builder,
75 @Qualifier(NFVO_DATA_SOURCE_QUALIFIER) final DataSource dataSource) {
76 return builder.dataSource(dataSource).packages(NfvoJob.class.getPackage().getName())
77 .persistenceUnit(PERSISTENCE_UNIT).build();
81 @Bean(name = "transactionManager")
82 public PlatformTransactionManager transactionManager(
83 @Qualifier("entityManagerFactory") final EntityManagerFactory entityManagerFactory) {
84 return new JpaTransactionManager(entityManagerFactory);