每次有新的 Http 請求時,我都需要更改目前使用的資料庫,並且其名稱是 Http URL 請求的一部分。 我正在嘗試使用以下程式碼,但是當我調用該服務並且它正在執行時,在前一個服務完成之前我無法再次調用,否則它會崩潰。 我注意到問題是“未選擇資料庫”,但我不知道如何更改資料庫而不崩潰:( 如果還沒有,則必須透過程式碼自動建立資料庫和表,因此我使用 Flyway。
我需要使用透過請求的 url 從使用者傳遞的名稱 網址模式是: .../api/v1/{db_name}/... 我正在使用 jdbcTemplate,但歡迎使用 JPA 解決方案:)
try { DriverManagerDataSource dataSource = (DriverManagerDataSource) jdbcTemplate.getDataSource(); String urlBase = env.getProperty("spring.datasource.url"); dataSource.setUrl(urlBase); dataSource.setSchema(name); Flyway flyway = Flyway.configure() .dataSource(dataSource) .defaultSchema(name) .load(); flyway.migrate(); dataSource.setUrl(urlBase+name); return true; }catch (Exception e){ e.printStackTrace(); }`
當我從兩個不同的客戶端呼叫連結兩次時出現異常:
StatementCallback; uncategorized SQLException for SQL [INSERT INTO statistiche_plu_giornaliere (id, id_dbLocale, idDevice, id_plu, descrizione, importo_totale, qta_totale_plu, qta_reso_storno, qta_reale, data, importo_sconti, importo_maggiorazioni, importo_reale, resi, id_reparto, tipo_riga) VALUES (0,256,'0AB1115556633569',0,'1LT PROSECCO',24.000000,2.000000,0.000000,2.000000,'',0.000000,0.000000,24.000000,0.000000,0,0)]; SQL state [3D000]; error code [1046]; No database selected; nested exception is java.sql.SQLException: No database selected at org.springframework.jdbc.core.JdbcTemplate.translateException(JdbcTemplate.java:1542) at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:393) at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:558) at com.trei.statistiche_cloud.db.DbHandler.updateStatisticheGiornaliere(DbHandler.java:119) at com.trei.statistiche_cloud.http.RestController.aggiornaDb(RestController.java:49) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1071) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:964) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) at javax.servlet.http.HttpServlet.service(HttpServlet.java:696) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) at javax.servlet.http.HttpServlet.service(HttpServlet.java:779) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214) at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:186) at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:354) at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:267) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) at org.springframework.session.web.http.SessionRepositoryFilter.doFilterInternal(SessionRepositoryFilter.java:142) at org.springframework.session.web.http.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:82) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:177) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360) at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399) at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:891) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1784) at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.base/java.lang.Thread.run(Thread.java:833) Caused by: java.sql.SQLException: No database selected at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129) at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) at com.mysql.cj.jdbc.StatementImpl.executeUpdateInternal(StatementImpl.java:1334) at com.mysql.cj.jdbc.StatementImpl.executeLargeUpdate(StatementImpl.java:2084) at com.mysql.cj.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1245) at org.springframework.jdbc.core.JdbcTemplateUpdateStatementCallback.doInStatement(JdbcTemplate.java:546) at org.springframework.jdbc.core.JdbcTemplateUpdateStatementCallback.doInStatement(JdbcTemplate.java:543) at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:381)`
我認為你的用例非常危險,因為你基本上是透過 HTTP/s 呼叫提供資料庫的秘密,其他不便之處包括:
為每個請求載入資料庫將顯著增加應用程式的回應時間
導致可擴充性和資源消耗問題。
相反,通常建議將 Spring Boot 應用程式設定為在啟動後立即啟動資料庫連接,並在程式的剩餘生命週期中保持其開啟。此策略可實現更高的可擴展性和更快的資料庫存取速度。
如果需要為同一個應用程式配置多個資料庫,您可以手動定義多個資料來源。根據每個請求,您可以選擇要查詢的資料庫