From 2e61fd31f6ebbc4f383b505257ea5d0f8bac12be Mon Sep 17 00:00:00 2001 From: gsd Date: Thu, 28 Sep 2023 23:52:19 +0300 Subject: [PATCH] replica update 1 --- .../app/configurations/DbConfiguration.java | 14 ++++ .../app/configurations/DbRoConfiguration.java | 72 +++++++++++++++++++ .../app/configurations/DbRwConfigurarion.java | 69 ++++++++++++++++++ .../java/app/services/ShardingService.java | 26 +++++++ src/main/java/app/services/db/BanService.java | 11 +-- src/main/java/app/services/db/DBService.java | 21 +++++- .../java/app/services/db/DetectService.java | 8 ++- .../app/services/db/DiscordAuthService.java | 15 ++-- .../java/app/services/db/DonateService.java | 5 +- .../java/app/services/db/FreeVIPService.java | 11 +-- .../app/services/db/PermitionService.java | 15 ++-- .../java/app/services/db/UsertimeService.java | 5 +- src/main/java/app/services/db/VIPService.java | 13 ++-- .../java/app/updates/BanCountUpdater.java | 6 +- .../java/app/updates/CountriesUpdater.java | 6 +- src/main/java/app/updates/UniqueUpdater.java | 5 +- .../java/app/updates/VipCountUpdater.java | 7 +- src/main/resources/application.yaml | 15 +++- 18 files changed, 282 insertions(+), 42 deletions(-) create mode 100644 src/main/java/app/configurations/DbConfiguration.java create mode 100644 src/main/java/app/configurations/DbRoConfiguration.java create mode 100644 src/main/java/app/configurations/DbRwConfigurarion.java create mode 100644 src/main/java/app/services/ShardingService.java diff --git a/src/main/java/app/configurations/DbConfiguration.java b/src/main/java/app/configurations/DbConfiguration.java new file mode 100644 index 0000000..434a6b1 --- /dev/null +++ b/src/main/java/app/configurations/DbConfiguration.java @@ -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(); +} diff --git a/src/main/java/app/configurations/DbRoConfiguration.java b/src/main/java/app/configurations/DbRoConfiguration.java new file mode 100644 index 0000000..cd5aaa4 --- /dev/null +++ b/src/main/java/app/configurations/DbRoConfiguration.java @@ -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 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; + } +} diff --git a/src/main/java/app/configurations/DbRwConfigurarion.java b/src/main/java/app/configurations/DbRwConfigurarion.java new file mode 100644 index 0000000..377ba72 --- /dev/null +++ b/src/main/java/app/configurations/DbRwConfigurarion.java @@ -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 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; + } +} diff --git a/src/main/java/app/services/ShardingService.java b/src/main/java/app/services/ShardingService.java new file mode 100644 index 0000000..ec89dd7 --- /dev/null +++ b/src/main/java/app/services/ShardingService.java @@ -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 getAccountsPerSteamID(SteamID steamID) { + //async here + return null; + } +} diff --git a/src/main/java/app/services/db/BanService.java b/src/main/java/app/services/db/BanService.java index 17c7aa4..167750a 100644 --- a/src/main/java/app/services/db/BanService.java +++ b/src/main/java/app/services/db/BanService.java @@ -8,6 +8,7 @@ import app.services.ServerService; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -15,14 +16,14 @@ import java.sql.Timestamp; import java.util.List; @Service -@Transactional +@Transactional(value = "RwTransactionManager") public class BanService { - @PersistenceContext EntityManager entityManager; ServerService serverService; @Autowired - public BanService(ServerService serverService) { + public BanService(ServerService serverService, @Qualifier(value = "RwEntityManager") EntityManager entityManager) { + this.entityManager = entityManager; this.serverService = serverService; } @@ -59,7 +60,7 @@ public class BanService { return (Long) entityManager.createNativeQuery("SELECT COUNT(*) FROM light_bans WHERE account_id = ?1 AND active = 0").getSingleResult(); } - @Transactional + @Transactional(value = "RwTransactionManager") public boolean removeBan(PlayerProfile user, PlayerProfile admin) { Ban ban = getBan(user.getSteamids()); if (ban == null) return false; @@ -70,7 +71,7 @@ public class BanService { .executeUpdate() > 0; } - @Transactional + @Transactional(value = "RwTransactionManager") public int addBan(PlayerProfile user, PlayerProfile admin, int ban_length, String ban_reason) { Ban ban = getBan(user.getSteamids()); if (ban != null) return -1 * ban.getId(); diff --git a/src/main/java/app/services/db/DBService.java b/src/main/java/app/services/db/DBService.java index b9130e9..2d12b68 100644 --- a/src/main/java/app/services/db/DBService.java +++ b/src/main/java/app/services/db/DBService.java @@ -2,15 +2,30 @@ package app.services.db; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; @Service public class DBService { - @PersistenceContext - EntityManager entityManager; + + EntityManager entityManager_rw; + EntityManager entityManager_ro; + + @Autowired + public DBService( + @Qualifier(value = "RwEntityManager") EntityManager entityManager_rw, + @Qualifier(value = "RoEntityManager") EntityManager entityManager_ro + ) { + this.entityManager_rw = entityManager_rw; + this.entityManager_ro = entityManager_ro; + } public Long getDBServerTime() { - return (Long) entityManager.createNativeQuery("SELECT UNIX_TIMESTAMP()") + Long rw_time = (Long) entityManager_rw.createNativeQuery("SELECT UNIX_TIMESTAMP()") + .getSingleResult(); + Long ro_time = (Long) entityManager_ro.createNativeQuery("SELECT UNIX_TIMESTAMP()") .getSingleResult(); + return rw_time; } } diff --git a/src/main/java/app/services/db/DetectService.java b/src/main/java/app/services/db/DetectService.java index 4c03c3c..3e5e7f4 100644 --- a/src/main/java/app/services/db/DetectService.java +++ b/src/main/java/app/services/db/DetectService.java @@ -7,6 +7,7 @@ import app.utils.SteamIDConverter; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import java.util.*; @@ -14,23 +15,24 @@ import java.util.*; @Service public class DetectService { - @PersistenceContext EntityManager entityManager; private Stats stats; @Autowired - public DetectService(Stats stats) { + public DetectService(Stats stats, @Qualifier(value = "RoEntityManager") EntityManager entityManager) { + this.entityManager = entityManager; this.stats = stats; } public List getAccountsPerSteamID(SteamID steamID) { List founded_steamid = new ArrayList<>(); for (Map.Entry stringServerEntry : stats.getServers().entrySet()) { - founded_steamid.addAll(entityManager.createNativeQuery("SELECT `steam_id` FROM `"+stringServerEntry.getValue().getDb()+"`."+"`user_connections` WHERE `connect_ip` in (SELECT `connect_ip` FROM `" + stringServerEntry.getValue().getDb() + "`." +"`user_connections` WHERE `steam_id` LIKE ?1 AND `connect_ip` NOT LIKE '10.%' ORDER BY `id` DESC) GROUP BY `steam_id`") + founded_steamid.addAll(entityManager.createNativeQuery("SELECT `steam_id` FROM `"+stringServerEntry.getValue().getDb()+"`."+"`user_connections` WHERE `connect_ip` in (SELECT `connect_ip` FROM `" + stringServerEntry.getValue().getDb() + "`." +"`user_connections` WHERE `steam_id` LIKE ?1 AND `connect_ip` NOT LIKE '10.%' GROUP BY `connect_ip` ORDER BY `id` DESC) GROUP BY `steam_id`") .setParameter(1, steamID.steam2) .getResultStream().map(String::valueOf).toList()); } + //SELECT `steam_id` FROM `tf2.facti13.five`.`user_connections` WHERE `connect_ip` in (SELECT `connect_ip` FROM `tf2.facti13.five`.`user_connections` WHERE `steam_id` LIKE 'STEAM_0:0:63666481' AND `connect_ip` NOT LIKE '10.%' GROUP BY `connect_ip` ORDER BY `id` DESC) GROUP BY `steam_id`; return founded_steamid.stream().distinct().map(SteamIDConverter::getSteamID).filter(Objects::nonNull).toList(); } diff --git a/src/main/java/app/services/db/DiscordAuthService.java b/src/main/java/app/services/db/DiscordAuthService.java index 6476985..f31e311 100644 --- a/src/main/java/app/services/db/DiscordAuthService.java +++ b/src/main/java/app/services/db/DiscordAuthService.java @@ -4,6 +4,8 @@ import app.entities.other.SteamID; import app.utils.SteamIDConverter; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -13,11 +15,16 @@ import java.util.List; import java.util.Map; @Service -@Transactional +@Transactional("RwTransactionManager") public class DiscordAuthService { - @PersistenceContext + EntityManager entityManager; + @Autowired + public DiscordAuthService(@Qualifier(value = "RwEntityManager") EntityManager entityManager) { + this.entityManager = entityManager; + } + private HashMap> processing_accounts = new HashMap<>(); public SteamID getSteamIDofDiscordID(String discord_id) { @@ -38,7 +45,7 @@ public class DiscordAuthService { .orElse(null); } - @Transactional + @Transactional("RwTransactionManager") public boolean setSteamIDofDiscordID(SteamID steamID, String discord_id) { return entityManager.createNativeQuery("INSERT INTO `steam2discord` (`id`, `steam_id`, `discord_id`, `timestamp`, `active`) VALUES (NULL, ?1, ?2, current_timestamp(), '1')") .setParameter(1, steamID.steam2) @@ -46,7 +53,7 @@ public class DiscordAuthService { .executeUpdate() > 0; } - @Transactional + @Transactional("RwTransactionManager") public boolean removeSteamIDofDiscordID(SteamID steamID) { return entityManager.createNativeQuery("UPDATE `steam2discord` SET `active` = '0' WHERE `steam_id` LIKE ?1") .setParameter(1, steamID.steam2) diff --git a/src/main/java/app/services/db/DonateService.java b/src/main/java/app/services/db/DonateService.java index 8f469c7..10b1251 100644 --- a/src/main/java/app/services/db/DonateService.java +++ b/src/main/java/app/services/db/DonateService.java @@ -9,6 +9,7 @@ import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import org.apache.commons.lang3.tuple.Pair; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; @@ -21,13 +22,13 @@ import java.util.Optional; @Component public class DonateService { - @PersistenceContext EntityManager entityManager; Stats stats; @Autowired - public DonateService(Stats stats) { + public DonateService(Stats stats, @Qualifier(value = "RoEntityManager") EntityManager entityManager) { + this.entityManager = entityManager; this.stats = stats; } diff --git a/src/main/java/app/services/db/FreeVIPService.java b/src/main/java/app/services/db/FreeVIPService.java index 65df6f6..ad1d7f2 100644 --- a/src/main/java/app/services/db/FreeVIPService.java +++ b/src/main/java/app/services/db/FreeVIPService.java @@ -8,6 +8,7 @@ import app.entities.server.Server; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -19,9 +20,9 @@ import java.util.List; import java.util.Map; @Service -@Transactional +@Transactional("RwTransactionManager") public class FreeVIPService{ - @PersistenceContext + EntityManager entityManager; UsertimeService usertimeService; @@ -30,7 +31,9 @@ public class FreeVIPService{ @Autowired public FreeVIPService(UsertimeService usertimeService, - VIPService vipService) { + VIPService vipService, + @Qualifier(value = "RwEntityManager") EntityManager entityManager) { + this.entityManager = entityManager; this.usertimeService = usertimeService; this.vipService = vipService; } @@ -69,7 +72,7 @@ public class FreeVIPService{ } } - @Transactional + @Transactional("RwTransactionManager") public boolean setGivedFreeVip(SteamID steamID, SocialAuth socialAuth) { if (socialAuth == null) socialAuth = new SocialAuth(); return entityManager.createNativeQuery("INSERT INTO `free_vip` (`id`, `steam3`, `vk_id`, `discord_id`, `date`) VALUES (NULL, ?1, ?2, ?3, current_timestamp())") diff --git a/src/main/java/app/services/db/PermitionService.java b/src/main/java/app/services/db/PermitionService.java index 07c8bc5..3a2a20c 100644 --- a/src/main/java/app/services/db/PermitionService.java +++ b/src/main/java/app/services/db/PermitionService.java @@ -6,17 +6,24 @@ import app.entities.db.Permition; import app.entities.other.SteamID; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; @Service -@Transactional +@Transactional("RwTransactionManager") public class PermitionService { - @PersistenceContext + EntityManager entityManager; + @Autowired + public PermitionService(@Qualifier(value = "RwEntityManager") EntityManager entityManager) { + this.entityManager = entityManager; + } + public Permition getPermition(SteamID steamID){ List result = entityManager.createNativeQuery("SELECT id, flags, immunity, status, amount, UNIX_TIMESTAMP(`reg_date`) as u_timestamp FROM sm_admins WHERE `identity` LIKE ?1") .setParameter(1, steamID.steam2) @@ -47,7 +54,7 @@ public class PermitionService { .executeUpdate(); } - @Transactional + @Transactional("RwTransactionManager") public boolean removePermition(SteamID steamID, String status) { if (getPermition(steamID) == null) return true; return entityManager.createNativeQuery("DELETE FROM sm_admins WHERE identity = ?1 AND status LIKE ?2") @@ -56,7 +63,7 @@ public class PermitionService { .executeUpdate() > 0; } - @Transactional + @Transactional("RwTransactionManager") public int extendPermition(SteamID steamID, Integer amount, String status) { return entityManager.createNativeQuery("UPDATE `sm_admins` SET `amount`=`amount`+?1, `reg_date`=`reg_date` WHERE `identity` LIKE ?2 AND `status` LIKE ?3") .setParameter(1, amount) diff --git a/src/main/java/app/services/db/UsertimeService.java b/src/main/java/app/services/db/UsertimeService.java index 4174e26..00e44b1 100644 --- a/src/main/java/app/services/db/UsertimeService.java +++ b/src/main/java/app/services/db/UsertimeService.java @@ -7,6 +7,7 @@ import app.utils.SteamIDConverter; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -18,7 +19,6 @@ import java.util.*; @Transactional public class UsertimeService { - @PersistenceContext EntityManager entityManager; //Лучше это всегда держать в true @@ -26,7 +26,8 @@ public class UsertimeService { Stats stats; @Autowired - public UsertimeService(Stats stats) { + public UsertimeService(Stats stats, @Qualifier(value = "RoEntityManager") EntityManager entityManager) { + this.entityManager = entityManager; this.stats = stats; } diff --git a/src/main/java/app/services/db/VIPService.java b/src/main/java/app/services/db/VIPService.java index 7ace1f3..00889ad 100644 --- a/src/main/java/app/services/db/VIPService.java +++ b/src/main/java/app/services/db/VIPService.java @@ -8,6 +8,7 @@ import app.utils.SteamIDConverter; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -18,9 +19,9 @@ import java.util.List; import java.util.Map; @Service -@Transactional +@Transactional("RwTransactionManager") public class VIPService { - @PersistenceContext + EntityManager entityManager; RestTemplate restTemplate; @@ -34,11 +35,13 @@ public class VIPService { @Autowired public VIPService(PermitionService permitionService, ServerService serverService, - DonateService donateService) { + DonateService donateService, + @Qualifier(value = "RwEntityManager") EntityManager entityManager) { this.restTemplate = new RestTemplate(); this.permitionService = permitionService; this.serverService = serverService; this.donateService = donateService; + this.entityManager = entityManager; } // Список ид из дискорда кто имеет платную випку @@ -59,7 +62,7 @@ public class VIPService { } // Транзакция, проверка и удаление випок подшедших к концу - @Transactional + @Transactional("RwTransactionManager") public int removeEndedVIPs() { List steamid2remove = entityManager.createNativeQuery("SELECT `identity` FROM `sm_admins` WHERE NOT UNIX_TIMESTAMP(`reg_date`) LIKE 0 AND `amount` NOT LIKE 0 AND `status` LIKE ?1 AND ((unix_timestamp(now()) - UNIX_TIMESTAMP(`reg_date`)) > `amount`)") .setParameter(1, "VIP") @@ -77,7 +80,7 @@ public class VIPService { } // Транзакция, добавление статистики - @Transactional + @Transactional("RwTransactionManager") public int regesterGiveVIP(SteamID steamID, int amount, VipGiveMethod vipGiveMethod, String extra, boolean extend) { return entityManager.createNativeQuery("INSERT INTO `gived_vip` (`id`, `steam2`, `amount`, `timestamp`, `givemethod`, `reserved`, `extend`) VALUES (NULL, ?1, ?2, current_timestamp(), ?3, ?4, ?5)") .setParameter(1, steamID.steam2) diff --git a/src/main/java/app/updates/BanCountUpdater.java b/src/main/java/app/updates/BanCountUpdater.java index 7e50174..a1b9b9c 100644 --- a/src/main/java/app/updates/BanCountUpdater.java +++ b/src/main/java/app/updates/BanCountUpdater.java @@ -7,6 +7,7 @@ import jakarta.persistence.PersistenceContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @@ -17,13 +18,14 @@ public class BanCountUpdater extends BaseUpdater{ private Stats stats; @Value("${backend.updates.ban_count}") boolean update = false; - @PersistenceContext + EntityManager entityManager; private final Logger logger = LoggerFactory.getLogger(BanCountUpdater.class); @Autowired - public BanCountUpdater(Stats stats) { + public BanCountUpdater(Stats stats, @Qualifier(value = "RoEntityManager") EntityManager entityManager) { + this.entityManager = entityManager; this.stats = stats; } diff --git a/src/main/java/app/updates/CountriesUpdater.java b/src/main/java/app/updates/CountriesUpdater.java index 1efae1f..0741d5c 100644 --- a/src/main/java/app/updates/CountriesUpdater.java +++ b/src/main/java/app/updates/CountriesUpdater.java @@ -10,6 +10,7 @@ import jakarta.persistence.PersistenceContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @@ -21,7 +22,6 @@ import java.util.Map; public class CountriesUpdater extends BaseUpdater{ Stats stats; GeoIP geoIP; - @PersistenceContext EntityManager entityManager; private final Logger logger = LoggerFactory.getLogger(CountriesUpdater.class); @@ -33,7 +33,9 @@ public class CountriesUpdater extends BaseUpdater{ @Autowired public CountriesUpdater(Stats stats, - GeoIP geoIP) { + GeoIP geoIP, + @Qualifier(value = "RoEntityManager") EntityManager entityManager) { + this.entityManager = entityManager; this.stats = stats; this.geoIP = geoIP; } diff --git a/src/main/java/app/updates/UniqueUpdater.java b/src/main/java/app/updates/UniqueUpdater.java index 893ba8c..a83d8f8 100644 --- a/src/main/java/app/updates/UniqueUpdater.java +++ b/src/main/java/app/updates/UniqueUpdater.java @@ -8,6 +8,7 @@ import jakarta.persistence.PersistenceContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @@ -17,7 +18,6 @@ import static java.time.Instant.now; public class UniqueUpdater extends BaseUpdater{ Stats stats; - @PersistenceContext EntityManager entityManager; private final String query_total = "SELECT DISTINCT `account_id` FROM `%s`.`user_connections` WHERE `connection_type` LIKE \"disconnect\" AND `connect_duration` > 300 UNION\n"; @@ -33,7 +33,8 @@ public class UniqueUpdater extends BaseUpdater{ boolean server_update = false; @Autowired - public UniqueUpdater(Stats stats) { + public UniqueUpdater(Stats stats, @Qualifier(value = "RoEntityManager") EntityManager entityManager) { + this.entityManager = entityManager; this.stats = stats; } diff --git a/src/main/java/app/updates/VipCountUpdater.java b/src/main/java/app/updates/VipCountUpdater.java index f1384a1..8a919b8 100644 --- a/src/main/java/app/updates/VipCountUpdater.java +++ b/src/main/java/app/updates/VipCountUpdater.java @@ -9,6 +9,7 @@ import jakarta.persistence.PersistenceContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @@ -18,7 +19,7 @@ import java.util.concurrent.CompletableFuture; @Component public class VipCountUpdater extends BaseUpdater{ private Stats stats; - @PersistenceContext + EntityManager entityManager; private VIPService vipService; @@ -33,10 +34,12 @@ public class VipCountUpdater extends BaseUpdater{ @Autowired public VipCountUpdater(Stats stats, VIPService vipService, - ServerService serverService) { + ServerService serverService, + @Qualifier(value = "RoEntityManager") EntityManager entityManager) { this.stats = stats; this.vipService = vipService; this.serverService = serverService; + this.entityManager = entityManager; } @PostConstruct diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 79ae20d..78622d6 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -13,8 +13,8 @@ spring: password: ${DB_PASSWORD} jpa: hibernate: - ddl-auto: none - show-sql: ${SHOW_SQL} + ddl-auto: ${DDL_AUTO:none} + show-sql: ${SHOW_SQL:false} org: jobrunr: @@ -53,6 +53,17 @@ backend: qiwi: ${VIP_QIWILINK} a2s: backend_url: ${A2S_BACKEND_URL} + db: + rw: + url: ${DB_MAIN_URL} + username: ${DB_MAIN_USERNAME} + password: ${DB_MAIN_PASSWORD} + driver-class-name: ${DB_MAIN_DRIVER} + ro: + url: ${DB_REPLICA_1_URL:none} + username: ${DB_REPLICA_1_USERNAME:none} + password: ${DB_REPLICA_1_PASSWORD:none} + driver-class-name: ${DB_REPLICA_1_DRIVER:none} logging: level: