Skip to content

Commit

Permalink
Merge pull request #59 from We-Food-C-I-A/hotfix/dbcp
Browse files Browse the repository at this point in the history
Hotfix/dbcp
  • Loading branch information
masiljangajji authored Aug 22, 2024
2 parents f0f4550 + 57c4f0c commit 30006a4
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 4 deletions.
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ dependencies {
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'

// dbcp
implementation 'com.mysql:mysql-connector-j'
implementation 'org.apache.commons:commons-dbcp2:2.9.0'

//AWS s3
implementation(platform("software.amazon.awssdk:bom:2.27.5"))
implementation("software.amazon.awssdk:s3")
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/wefood/back/BackApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;

@SpringBootApplication
@ConfigurationPropertiesScan
public class BackApplication {

public static void main(String[] args) {
Expand Down
97 changes: 97 additions & 0 deletions src/main/java/com/wefood/back/config/DatabaseConfig.java
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 src/main/java/com/wefood/back/config/DatabaseProperties.java
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;
}
13 changes: 9 additions & 4 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ cloud.aws.stack.auto-=false
image.service.impl=s3Service


spring.datasource.url=${jdbc.url}
spring.datasource.username=${jdbc.username}
spring.datasource.password=${jdbc.password}
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
### mysql
database.mysql.url=${jdbc.url}
database.mysql.userName=${jdbc.username}
database.mysql.password=${jdbc.password}
database.mysql.initialSize=10
database.mysql.maxTotal=10
database.mysql.minIdle=10
database.mysql.maxIdle=10
database.mysql.maxWait=1000

spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=100MB

0 comments on commit 30006a4

Please sign in to comment.