Tukar pangkalan data menggunakan Url permintaan Http separa semasa pelaksanaan Spring Boot
P粉663883862
P粉663883862 2024-03-31 12:55:54
0
1
447

Setiap kali terdapat permintaan Http baharu, saya perlu menukar pangkalan data yang sedang digunakan dan namanya adalah sebahagian daripada permintaan URL Http. Saya cuba menggunakan kod berikut, tetapi apabila saya memanggil perkhidmatan dan ia sedang dilaksanakan, saya tidak boleh memanggilnya semula sehingga perkhidmatan sebelumnya selesai atau ia ranap. Saya perasan masalahnya ialah "Tiada pangkalan data yang dipilih" tetapi saya tidak tahu cara menukar pangkalan data tanpa ranap :( Jika anda belum memilikinya, anda perlu mencipta pangkalan data dan jadual secara automatik melalui kod, jadi saya menggunakan Flyway.

Saya perlu menggunakan nama yang diluluskan daripada pengguna melalui url yang diminta Corak URL ialah: .../api/v1/{db_name}/... Saya menggunakan jdbcTemplate, tetapi penyelesaian JPA dialu-alukan :)

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();
   }`

Pengecualian apabila saya menghubungi pautan dua kali daripada dua pelanggan berbeza:

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)`

P粉663883862
P粉663883862

membalas semua(1)
P粉545682500

Saya rasa kes penggunaan anda sangat berbahaya kerana pada asasnya anda menyediakan rahsia pangkalan data melalui panggilan HTTP/s, kesulitan lain termasuk:

  1. Memuatkan pangkalan data untuk setiap permintaan akan meningkatkan masa respons aplikasi anda dengan ketara

  2. Menyebabkan masalah skalabiliti dan penggunaan sumber.

Sebaliknya, biasanya disyorkan untuk menyediakan aplikasi Spring Boot untuk memulakan sambungan pangkalan data serta-merta selepas permulaan dan memastikan ia terbuka untuk baki kitaran hayat program. Strategi ini membolehkan skalabiliti yang lebih besar dan akses pangkalan data yang lebih pantas.

Jika anda perlu mengkonfigurasi berbilang pangkalan data untuk aplikasi yang sama, anda boleh mentakrifkan berbilang sumber data secara manual. Berdasarkan setiap permintaan, anda boleh memilih pangkalan data untuk membuat pertanyaan

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan