묻고 답해요
141만명의 커뮤니티!! 함께 토론해봐요.
인프런 TOP Writers
-
해결됨[스프링 배치 입문] 예제로 배우는 핵심 Spring Batch
다중DB 접속 설정 에러
안녕하세요!강의 잘 봤습니다!강의 보고 다중DB를 설정하는과정에서 에러가나서 질문드려요여기서 하면 안되는거같은데 ... 도움부탁드릴려고 글 남깁니다!targetDb에서 select는 정상적으로 들어오는데 localDB save 할때 할당 문제인거 같은데 ...에로코드org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is java.lang.IllegalStateException: Already value [org.springframework.jdbc.datasource.ConnectionHolder@4ff98dfc] for key [HikariDataSource (HikariPool-1)] bound to thread [scheduling-1] at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:467) ~[spring-orm-5.3.6.jar:5.3.6] at org.springframework.transaction.support.AbstractPlatformTransactionManager.startTransaction(AbstractPlatformTransactionManager.java:400) ~[spring-tx-5.3.6.jar:5.3.6] at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373) ~[spring-tx-5.3.6.jar:5.3.6] at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:595) ~[spring-tx-5.3.6.jar:5.3.6] at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:382) ~[spring-tx-5.3.6.jar:5.3.6] ------ at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) [spring-context-5.3.6.jar:5.3.6] at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) [spring-context-5.3.6.jar:5.3.6] at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_181] at java.util.concurrent.FutureTask.runAndReset$$$capture(FutureTask.java:308) [na:1.8.0_181] at java.util.concurrent.FutureTask.runAndReset(FutureTask.java) [na:1.8.0_181] at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_181] at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) [na:1.8.0_181] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_181] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_181] at java.lang.Thread.run(Thread.java:748) [na:1.8.0_181] Caused by: java.lang.IllegalStateException: Already value [org.springframework.jdbc.datasource.ConnectionHolder@4ff98dfc] for key [HikariDataSource (HikariPool-1)] bound to thread [scheduling-1] at org.springframework.transaction.support.TransactionSynchronizationManager.bindResource(TransactionSynchronizationManager.java:193) ~[spring-tx-5.3.6.jar:5.3.6] at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:442) ~[spring-orm-5.3.6.jar:5.3.6] ... 93 common frames omitted 2024-02-26 12:16:27.150 INFO 23616 --- [ scheduling-1] o.s.batch.core.step.AbstractStep : Step: [step] executed in 737ms 2024-02-26 12:16:27.160 INFO 23616 --- [ scheduling-1] c.m.mj_agent.listener.JobLoggerListener : taragetJob Job is Done. (Status: FAILED) 2024-02-26 12:16:27.160 INFO 23616 --- [ scheduling-1] c.m.mj_agent.listener.JobLoggerListener : Job is Failed 2024-02-26 12:16:27.168 INFO 23616 --- [ scheduling-1] o.s.b.c.l.support.SimpleJobLauncher : Job: [SimpleJob: [name=taragetJob]] completed with the following parameters: [{time=1708917386263}] and the following status: [FAILED] in 797ms localDbConfigpackage com.munjaon.mj_agent.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.PropertySource; 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 @PropertySource({ "classpath:application.yml" }) @EnableJpaRepositories( // basePackages = {"com.repia.collectweb.common", "com.repia.collectweb.schedule"}, // basePackages = {"com.munjaon.mj_agent.batch.local.repo"}, basePackages = {"com.munjaon.mj_agent.batch.local"}, entityManagerFactoryRef = "primaryEntityManager", transactionManagerRef = "primaryTransactionManager" ) public class LocalDbConfig { @Autowired private Environment env; @Bean @Primary @ConfigurationProperties(prefix = "spring.local-datasource.datasource.hikari") public DataSource primaryDataSource() { return DataSourceBuilder.create().build(); } @Bean @Primary public LocalContainerEntityManagerFactoryBean primaryEntityManager() { LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); em.setDataSource(primaryDataSource()); em.setPackagesToScan(new String[] { "com.munjaon.mj_agent.batch.local.repo.entity" }); HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); em.setJpaVendorAdapter(vendorAdapter); HashMap<String, Object> properties = new HashMap<>(); properties.put("hibernate.show_sql", env.getProperty("spring.jpa.show_sql")); properties.put("hibernate.format_sql", env.getProperty("spring.jpa.format_sql")); properties.put("hibernate.use_sql_comments", env.getProperty("spring.jpa.use_sql_comments")); properties.put("hibernate.hbm2ddl.auto", env.getProperty("spring.jpa.hibernate.hbm2ddl.auto")); properties.put("hibernate.dialect", env.getProperty("spring.jpa.database-platform")); // properties.put("hibernate.dialect", env.getProperty("spring.jpa.database")); em.setJpaPropertyMap(properties); return em; } @Bean @Primary public PlatformTransactionManager primaryTransactionManager() { JpaTransactionManager transactionManager = new JpaTransactionManager(); transactionManager.setEntityManagerFactory(primaryEntityManager().getObject()); return transactionManager; } }TargetDbConfigpackage com.munjaon.mj_agent.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; 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 org.springframework.transaction.annotation.EnableTransactionManagement; import javax.sql.DataSource; import java.util.HashMap; import java.util.Objects; /** * */ @Configuration @PropertySource({ "classpath:application.yml" }) @EnableTransactionManagement @EnableJpaRepositories( basePackages = {"com.munjaon.mj_agent.batch.target.repo"} , entityManagerFactoryRef = "targetDbEntityManager" , transactionManagerRef = "targetDbTransactionManager" ) public class TargetDbConfig { @Autowired private Environment env; @Bean // @Primary public LocalContainerEntityManagerFactoryBean targetDbEntityManager() { LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); em.setDataSource(targetDbDataSource()); em.setPackagesToScan(new String[] { "com.munjaon.mj_agent.batch.target.repo.entity" }); HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); em.setJpaVendorAdapter(vendorAdapter); HashMap<String, Object> properties = new HashMap<>(); properties.put("hibernate.show_sql", env.getProperty("spring.jpa.show_sql")); properties.put("hibernate.format_sql", env.getProperty("spring.jpa.format_sql")); properties.put("hibernate.use_sql_comments", env.getProperty("spring.jpa.use_sql_comments")); // properties.put("hibernate.hbm2ddl.auto", false); // properties.put("hibernate.dialect", false); properties.put("hibernate.hbm2ddl.auto", env.getProperty("spring.jpa.hibernate.hbm2ddl.auto")); properties.put("hibernate.dialect", env.getProperty("spring.jpa.database-platform")); em.setJpaPropertyMap(properties); return em; } @Bean // @Primary @ConfigurationProperties(prefix = "spring.target-datasource.datasource.hikari") public DataSource targetDbDataSource() { return DataSourceBuilder.create().build(); } // @Primary // @Bean @Bean(name = "targetDbTransactionManager") public PlatformTransactionManager targetDbTransactionManager(EntityManagerFactoryBuilder builder) { return new JpaTransactionManager(Objects.requireNonNull(targetDbEntityManager().getObject())); } } 감사합니다!
-
미해결실전! Querydsl
queryDSL 다중DB에 대해서 질문드립니다.
안녕하세요 선생님의 querydsl 강의를 수강하고 현업에서 적용을 하고 있는 개발자입니다. 위의 sql은 2개의 다른DB의 테이블을 조인하는 쿼리문인데요. fenoteDB.st_send는 fenoteDB의 st_send 테이블이고fe_order는 femarketDB의 fe_order 테이블입니다. 이를 queryDSL로 바꾸는 작업을 하고 있는데 구글링을 많이 해봤지만 2개의 DB의 경우 서로다른 엔티티를 사용하는 경우에 대한 자료만 나오고 따라서 위의 사진과 같은 쿼리를 querydsl로 바꾸는 자료가 나오질 않는데요. 계속 방법을 찾다 결국 영한 선생님께 도움을 청하게 되었습니다..혹시 이와 관련된 자료가 있으면 부탁드려도 될까요? 아니면 아예 방법이 없는 건지 알고 싶습니다!