blob: 46f2b0e06201b476dfc90722aecf3ffe93b5c43d [file] [log] [blame]
Aharoni, Pavel (pa0916)ca3cb012018-10-22 15:29:57 +03001/*-
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
23package org.onap.osam.config;
24
25
26import org.hibernate.SessionFactory;
27import org.onap.portalsdk.core.service.DataAccessService;
28import org.onap.portalsdk.core.service.DataAccessServiceImpl;
29import org.springframework.beans.factory.annotation.Autowired;
30import org.springframework.context.annotation.Bean;
31import org.springframework.context.annotation.Configuration;
32import org.springframework.jdbc.datasource.DriverManagerDataSource;
33import org.springframework.orm.hibernate4.HibernateTransactionManager;
34import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
35import org.springframework.transaction.PlatformTransactionManager;
36import org.springframework.transaction.annotation.EnableTransactionManagement;
37
38import javax.sql.DataSource;
39import java.util.Properties;
40
41@Configuration
42@EnableTransactionManagement
43public class DataSourceConfig {
44
45 @Bean
46 @Autowired
47 public LocalSessionFactoryBean sessionFactory(DataSource dataSource) {
48 LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
49 sessionFactory.setDataSource(dataSource);
50 //I used this class org.openecomp.portalsdk.core.conf.HibernateConfiguration to learn how to config the session factory
51 // and use the following url for actual h2 properties
52 //https://github.com/levi-putna/Hibernate-H2-Example/blob/master/hibernate-h2-example/src/hibernate.cfg.xml
53 Properties properties = getH2Properties();
54
55 properties.put("hibernate.default_schema", "PUBLIC");
56 properties.put("connection.pool_size", 10);
57 properties.put("cache.provider_class", "org.hibernate.cache.internal.NoCacheProvider");
58 properties.put("hibernate.show_sql", false);
59 properties.put("hbm2ddl.auto", "create");
60 properties.put("hibernate.hbm2ddl.auto", "create");
61
62 sessionFactory.setHibernateProperties(properties);
63 sessionFactory.setPackagesToScan("org.onap");
64 return sessionFactory;
65 }
66
67 @Bean
68 public DataSource getDataSource() {
69 DriverManagerDataSource dataSource = new DriverManagerDataSource();
70 dataSource.setDriverClassName("org.h2.Driver");
71 dataSource.setUrl("jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1");
72 dataSource.setUsername("sa");
73 dataSource.setPassword("");
74 return dataSource;
75 }
76
77 public Properties getH2Properties() {
78 Properties properties = new Properties();
79 properties.put("dialect", "org.hibernate.dialect.H2Dialect");
80 return properties;
81 }
82
83 public Properties getSqliteProperties() {
84 Properties properties = new Properties();
85 properties.put("connection.driver_class", "org.sqlite.JDBC");
86 properties.put("connection.url", "jdbc:sqlite:memory:myDb");
87 properties.put("connection.username", "sa");
88 properties.put("connection.password", "sa");
89 properties.put("dialect", "com.enigmabridge.hibernate.dialect.SQLiteDialect");
90 return properties;
91 }
92
93 @Bean
94 public DataAccessService dataAccessService() {
95 return new DataAccessServiceImpl();
96 }
97
98 @Bean
99 @Autowired
100 public PlatformTransactionManager transactionManager(SessionFactory sessionFactory) {
101 return new HibernateTransactionManager(sessionFactory);
102 }
103}