-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #59 from We-Food-C-I-A/hotfix/dbcp
Hotfix/dbcp
- Loading branch information
Showing
5 changed files
with
132 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package com.wefood.back.config; | ||
|
||
import jakarta.persistence.EntityManagerFactory; | ||
import org.apache.commons.dbcp2.BasicDataSource; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.orm.jpa.JpaTransactionManager; | ||
import org.springframework.orm.jpa.JpaVendorAdapter; | ||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; | ||
import org.springframework.orm.jpa.vendor.Database; | ||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
|
||
import javax.sql.DataSource; | ||
import java.util.Properties; | ||
|
||
/** | ||
* packageName : store.mybooks.resource.config | ||
* fileName : DatabaseConfig | ||
* author : Fiat_lux | ||
* date : 2/20/24 | ||
* description : | ||
* =========================================================== | ||
* DATE AUTHOR NOTE | ||
* ----------------------------------------------------------- | ||
* 2/20/24 Fiat_lux 최초 생성 | ||
*/ | ||
@Configuration | ||
public class DatabaseConfig { | ||
private final DatabaseProperties databaseProperties; | ||
|
||
@Autowired | ||
public DatabaseConfig(DatabaseProperties databaseProperties) { | ||
this.databaseProperties = databaseProperties; | ||
} | ||
|
||
@Bean | ||
public DataSource dataSource() { | ||
BasicDataSource dataSource = new BasicDataSource(); | ||
|
||
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver"); | ||
dataSource.setUrl(databaseProperties.getUrl()); | ||
dataSource.setUsername(databaseProperties.getUserName()); | ||
dataSource.setPassword(databaseProperties.getPassword()); | ||
|
||
dataSource.setInitialSize(databaseProperties.getInitialSize()); | ||
dataSource.setMaxTotal(databaseProperties.getMaxTotal()); | ||
dataSource.setMinIdle(databaseProperties.getMinIdle()); | ||
dataSource.setMaxIdle(databaseProperties.getMaxIdle()); | ||
|
||
dataSource.setMaxWaitMillis(databaseProperties.getMaxWait()); | ||
|
||
dataSource.setTestOnBorrow(true); | ||
dataSource.setTestOnReturn(true); | ||
dataSource.setTestWhileIdle(true); | ||
|
||
return dataSource; | ||
} | ||
|
||
@Bean | ||
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) { | ||
LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean(); | ||
emf.setDataSource(dataSource); | ||
emf.setPackagesToScan("com.wefood.back"); | ||
emf.setJpaVendorAdapter(jpaVendorAdapters()); | ||
emf.setJpaProperties(jpaProperties()); | ||
|
||
return emf; | ||
} | ||
|
||
private JpaVendorAdapter jpaVendorAdapters() { | ||
HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter(); | ||
hibernateJpaVendorAdapter.setDatabase(Database.MYSQL); | ||
return hibernateJpaVendorAdapter; | ||
} | ||
|
||
private Properties jpaProperties() { | ||
Properties jpaProperties = new Properties(); | ||
jpaProperties.setProperty("hibernate.show_sql", "false"); | ||
jpaProperties.setProperty("hibernate.format_sql", "true"); | ||
jpaProperties.setProperty("hibernate.use_sql_comments", "true"); | ||
jpaProperties.setProperty("hibernate.globally_quoted_identifiers", "true"); | ||
jpaProperties.setProperty("hibernate.temp.use_jdbc_metadata_defaults", "false"); | ||
|
||
return jpaProperties; | ||
} | ||
|
||
@Bean | ||
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) { | ||
JpaTransactionManager transactionManager = new JpaTransactionManager(); | ||
transactionManager.setEntityManagerFactory(entityManagerFactory); | ||
|
||
return transactionManager; | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/wefood/back/config/DatabaseProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.wefood.back.config; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
|
||
@ConfigurationProperties(prefix = "database.mysql") | ||
@Getter | ||
@Setter | ||
public class DatabaseProperties { | ||
private String url; | ||
private String userName; | ||
private String password; | ||
private Integer initialSize; | ||
private Integer maxTotal; | ||
private Integer minIdle; | ||
private Integer maxIdle; | ||
private Integer maxWait; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters