d27c891304b45e03a10524735d0a0c4da8cb1c0e
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.adapters.requestsdb.application;
24
25
26 import javax.persistence.EntityManagerFactory;
27 import javax.sql.DataSource;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.beans.factory.annotation.Qualifier;
30 import org.springframework.boot.context.properties.ConfigurationProperties;
31 import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
32 import org.springframework.context.annotation.Bean;
33 import org.springframework.context.annotation.Configuration;
34 import org.springframework.context.annotation.Primary;
35 import org.springframework.context.annotation.Profile;
36 import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
37 import org.springframework.jmx.export.MBeanExporter;
38 import org.springframework.orm.jpa.JpaTransactionManager;
39 import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
40 import org.springframework.transaction.PlatformTransactionManager;
41 import org.springframework.transaction.annotation.EnableTransactionManagement;
42 import com.zaxxer.hikari.HikariConfig;
43 import com.zaxxer.hikari.HikariDataSource;
44
45 @Profile({"!test"})
46 @Configuration
47 @EnableTransactionManagement
48 @EnableJpaRepositories(entityManagerFactoryRef = "requestEntityManagerFactory",
49         transactionManagerRef = "requestTransactionManager", basePackages = {"org.onap.so.db.request.data.repository"})
50 public class RequestDBConfig {
51
52     @Autowired(required = false)
53     private MBeanExporter mBeanExporter;
54
55     @Bean
56     @ConfigurationProperties(prefix = "spring.datasource.hikari")
57     public HikariConfig requestDbConfig() {
58         return new HikariConfig();
59     }
60
61     @Bean(name = "requestDataSource")
62     public DataSource dataSource() {
63         if (mBeanExporter != null) {
64             mBeanExporter.addExcludedBean("requestDataSource");
65         }
66         HikariConfig hikariConfig = this.requestDbConfig();
67         return new HikariDataSource(hikariConfig);
68     }
69
70
71     @Primary
72     @Bean(name = "requestEntityManagerFactory")
73     public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder,
74             @Qualifier("requestDataSource") DataSource dataSource) {
75         return builder.dataSource(dataSource).packages("org.onap.so.db.request.beans").persistenceUnit("requestDB")
76                 .build();
77     }
78
79     @Primary
80     @Bean(name = "requestTransactionManager")
81     public PlatformTransactionManager transactionManager(
82             @Qualifier("requestEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
83         return new JpaTransactionManager(entityManagerFactory);
84     }
85
86 }