18 changed files with 282 additions and 42 deletions
@ -0,0 +1,14 @@ |
|||
package app.configurations; |
|||
|
|||
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; |
|||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; |
|||
import org.springframework.transaction.PlatformTransactionManager; |
|||
|
|||
import javax.sql.DataSource; |
|||
|
|||
public interface DbConfiguration { |
|||
public DataSourceProperties DataSourceProperties(); |
|||
public DataSource DataSource(); |
|||
public LocalContainerEntityManagerFactoryBean EntityManager(); |
|||
public PlatformTransactionManager TransactionManager(); |
|||
} |
@ -0,0 +1,72 @@ |
|||
package app.configurations; |
|||
|
|||
import com.zaxxer.hikari.HikariDataSource; |
|||
import org.slf4j.Logger; |
|||
import org.slf4j.LoggerFactory; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; |
|||
import org.springframework.boot.context.properties.ConfigurationProperties; |
|||
import org.springframework.context.annotation.Bean; |
|||
import org.springframework.context.annotation.Configuration; |
|||
import org.springframework.context.annotation.Primary; |
|||
import org.springframework.context.annotation.Scope; |
|||
import org.springframework.core.env.Environment; |
|||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; |
|||
import org.springframework.orm.jpa.JpaTransactionManager; |
|||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; |
|||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; |
|||
import org.springframework.transaction.PlatformTransactionManager; |
|||
|
|||
import javax.sql.DataSource; |
|||
import java.util.HashMap; |
|||
|
|||
@Configuration |
|||
@EnableJpaRepositories(basePackages = "app.entities.dummy.ro", entityManagerFactoryRef = "RoEntityManager", transactionManagerRef = "RoTransactionManager") |
|||
public class DbRoConfiguration implements DbConfiguration { |
|||
@Autowired |
|||
Environment environment; |
|||
|
|||
private final Logger logger = LoggerFactory.getLogger(this.getClass()); |
|||
|
|||
@Bean(name = "RoDataSourceProperties") |
|||
@Scope(value = "singleton") |
|||
@ConfigurationProperties(prefix = "backend.db.ro") |
|||
public DataSourceProperties DataSourceProperties() { |
|||
DataSourceProperties dataSourceProperties = new DataSourceProperties(); |
|||
/*if (dataSourceProperties.getUrl().equals("none") && dataSourceProperties.getUsername().equals("none") && dataSourceProperties.getPassword().equals("none")) { |
|||
logger.warn("R/O EntityManager need disabled, use default rw"); |
|||
return (new DbRwConfigurarion()).DataSourceProperties(); |
|||
}*/ |
|||
return dataSourceProperties; |
|||
} |
|||
|
|||
@Bean(name = "RoDataSource") |
|||
@Scope(value = "singleton") |
|||
public DataSource DataSource() { |
|||
HikariDataSource dataSource = DataSourceProperties().initializeDataSourceBuilder().type(HikariDataSource.class).build(); |
|||
dataSource.setReadOnly(true); |
|||
return dataSource; |
|||
} |
|||
|
|||
@Bean(name = "RoEntityManager") |
|||
@Scope(value = "singleton") |
|||
public LocalContainerEntityManagerFactoryBean EntityManager() { |
|||
LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean(); |
|||
entityManager.setDataSource(DataSource()); |
|||
entityManager.setPackagesToScan("app.entities.dummy.ro"); |
|||
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); |
|||
entityManager.setJpaVendorAdapter(vendorAdapter); |
|||
HashMap<String, Object> properties = new HashMap<>(); |
|||
properties.put("hibernate.hbm2ddl.auto", environment.getProperty("DDL_AUTO", "none")); |
|||
entityManager.setJpaPropertyMap(properties); |
|||
return entityManager; |
|||
} |
|||
|
|||
@Bean(name = "RoTransactionManager") |
|||
@Scope(value = "singleton") |
|||
public PlatformTransactionManager TransactionManager() { |
|||
JpaTransactionManager transactionManager = new JpaTransactionManager(); |
|||
transactionManager.setEntityManagerFactory(EntityManager().getObject()); |
|||
return transactionManager; |
|||
} |
|||
} |
@ -0,0 +1,69 @@ |
|||
package app.configurations; |
|||
|
|||
import com.zaxxer.hikari.HikariDataSource; |
|||
import org.slf4j.Logger; |
|||
import org.slf4j.LoggerFactory; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; |
|||
import org.springframework.boot.context.properties.ConfigurationProperties; |
|||
import org.springframework.context.annotation.Bean; |
|||
import org.springframework.context.annotation.Configuration; |
|||
import org.springframework.context.annotation.Primary; |
|||
import org.springframework.context.annotation.Scope; |
|||
import org.springframework.core.env.Environment; |
|||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; |
|||
import org.springframework.orm.hibernate5.LocalSessionFactoryBean; |
|||
import org.springframework.orm.jpa.JpaTransactionManager; |
|||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; |
|||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; |
|||
import org.springframework.transaction.PlatformTransactionManager; |
|||
|
|||
import javax.sql.DataSource; |
|||
import java.util.HashMap; |
|||
|
|||
@Configuration |
|||
@EnableJpaRepositories(basePackages = "app.entities.dummy.rw", entityManagerFactoryRef = "RwEntityManager", transactionManagerRef = "RwTransactionManager") |
|||
public class DbRwConfigurarion implements DbConfiguration{ |
|||
|
|||
@Autowired |
|||
Environment environment; |
|||
|
|||
private final Logger logger = LoggerFactory.getLogger(this.getClass()); |
|||
|
|||
@Bean(name = "RwDataSourceProperties") |
|||
@Scope(value = "singleton") |
|||
@ConfigurationProperties(prefix = "backend.db.rw") |
|||
public DataSourceProperties DataSourceProperties() { |
|||
return new DataSourceProperties(); |
|||
} |
|||
|
|||
@Bean(name = "RwDataSource") |
|||
@Scope(value = "singleton") |
|||
public DataSource DataSource() { |
|||
HikariDataSource dataSource = DataSourceProperties().initializeDataSourceBuilder().type(HikariDataSource.class).build(); |
|||
dataSource.setReadOnly(false); |
|||
return dataSource; |
|||
} |
|||
|
|||
@Bean(name = "RwEntityManager") |
|||
@Scope(value = "singleton") |
|||
public LocalContainerEntityManagerFactoryBean EntityManager() { |
|||
LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean(); |
|||
entityManager.setDataSource(DataSource()); |
|||
entityManager.setPackagesToScan("app.entities.dummy.rw"); |
|||
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); |
|||
entityManager.setJpaVendorAdapter(vendorAdapter); |
|||
HashMap<String, Object> properties = new HashMap<>(); |
|||
properties.put("hibernate.hbm2ddl.auto", environment.getProperty("DDL_AUTO", "none")); |
|||
entityManager.setJpaPropertyMap(properties); |
|||
return entityManager; |
|||
} |
|||
|
|||
@Bean(name = "RwTransactionManager") |
|||
@Scope(value = "singleton") |
|||
public PlatformTransactionManager TransactionManager() { |
|||
JpaTransactionManager transactionManager = new JpaTransactionManager(); |
|||
transactionManager.setEntityManagerFactory(EntityManager().getObject()); |
|||
return transactionManager; |
|||
} |
|||
} |
@ -0,0 +1,26 @@ |
|||
package app.services; |
|||
|
|||
import app.entities.other.SteamID; |
|||
import jakarta.persistence.EntityManager; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.beans.factory.annotation.Qualifier; |
|||
import org.springframework.stereotype.Service; |
|||
|
|||
import java.util.List; |
|||
|
|||
@Service |
|||
public class ShardingService { |
|||
|
|||
@Autowired |
|||
@Qualifier(value = "RwEntityManager") |
|||
EntityManager entityManager_rw; |
|||
|
|||
@Autowired |
|||
@Qualifier(value = "RoEntityManager") |
|||
EntityManager entityManager_ro; |
|||
|
|||
public List<SteamID> getAccountsPerSteamID(SteamID steamID) { |
|||
//async here
|
|||
return null; |
|||
} |
|||
} |
Loading…
Reference in new issue