在Spring Boot中EntityManager不会通过DatasourceBuilder更新

时间:2018-08-14 09:23:38

标签: java hibernate spring-boot jpa

我正在使用弹簧靴。我想尝试动态更改数据库。但是,如果我更改DataSourceBuilder,则EntityManger不会更新。

      @PersistenceContext
        private EntityManager em;
        public ScraperProxy fetchActData(String jurStr, Long accountNo, String proxyIp) {
            if (accountNo == 1) {

                getDataSource("org.postgresql.Driver", "jdbc:postgresql://192.168.0.21:5432/YYYYY01", "abc", "1234");
                BigInteger l = (BigInteger) em.createNativeQuery("select count(b.id) from admin.borrower_mstr b")
                        .getSingleResult();
            } else {
                getDataSource("com.mysql.jdbc.Driver", "jdbc:mysql://192.168.0.22:3306/XXXX01", "root", "root");

                BigInteger l = (BigInteger) em.createNativeQuery("select count(a.id) from sbic_01.activity_mstr a")
                        .getSingleResult();
                }
             }



        public DataSource getDataSource(String driverClass, String DBB_URL, String user, String password) {
            DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
            dataSourceBuilder.url(DBB_URL);
            dataSourceBuilder.username(user);
            dataSourceBuilder.password(password);
            dataSourceBuilder.driverClassName(driverClass);
            return dataSourceBuilder.build();
        }

和application.properties,

spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://192.168.0.21:5432/XXXXX01
spring.datasource.username=abc
spring.datasource.password=1234

所以我出错了,

    Hibernate: select count(a.id) from sbic_01.activity_mstr a
    2018-08-14 14:38:20.913  WARN 13045 --- [xecutor_thread1] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 42P01
    2018-08-14 14:38:20.913 ERROR 13045 --- [xecutor_thread1] o.h.engine.jdbc.spi.SqlExceptionHelper   : ERROR: relation "sbic_01.activity_mstr" does not exist
      Position: 25
    Exception in thread "default_task_executor_thread1" javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
        at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1692)
   org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
        at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
        at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
        at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)

        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)
    Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSe

请帮助我,我花了很多时间仍然遇到错误。

0 个答案:

没有答案