Spring boot2. X project, use the data jpa framework, the common method can achieve data source switch, but the new thread and timing task execution method, but can't insert and modify the database, and control of the log output, you can query data,
The same data source configuration, use mybatis framework has no such problem,
CodePudding user response:
People? They found this problem?
CodePudding user response:
How do you select the data source configuration is configured? Code and configuration files
From the point of view of the phenomenon of your choice of data source logic Session, when performing the logic of a Session, some information on the ThreadLocal, and in the new thread and timing task thread, there won't be initialized ThreadLocal process, so fail,
Provide train of thought, hope I can help
CodePudding user response:
refer to the second floor BoRoBoRoMe response: how you select the data source configuration configuration? Code and configuration files From the point of view of the phenomenon of your choice of data source logic Session, when performing the logic of a Session, some information on the ThreadLocal, and in the new thread and timing task thread, there won't be initialized ThreadLocal process, so fail, Provide ideas, hope can help The following is the main configuration and method The configuration file Spring: Application: Name: service - system Hikari: First: JDBC - url: JDBC: mysql://? useUnicode=true& CharacterEncoding=utf8 & amp; UseSSL=false Username: Password: The driver - class - name: com. Mysql.. JDBC driver Minimum - idle: 50 Maximum - the pool - size: 200 Idle - timeout: 30000 Max - lifetime: 300000 Connection timeout: 18000 Connection - the test - query: select 1 Second: JDBC - url: JDBC: mysql://? useUnicode=true& CharacterEncoding=utf8 & amp; UseSSL=false Username: Password: The driver - class - name: com. Mysql.. JDBC driver Third: JDBC - url: JDBC: mysql://? useUnicode=true& CharacterEncoding=utf8 & amp; UseSSL=false Username: Password: The driver - class - name: com. Mysql.. JDBC driver Minimum - idle: 50 Maximum - the pool - size: 200 Idle - timeout: 30000 Max - lifetime: 300000 Connection timeout: 18000 Connection - the test - query: select 1 Methods: /* * * the actual data source configuration */ @ Data @ Component @ ConfigurationProperties (prefix="spring. Hikari") Public class DBProperties { Private HikariDataSource first; Private HikariDataSource second; Private HikariDataSource third; } /* * * data source configuration */ @ Configuration Public class DataSourceConfig { The @autowired Private DBProperties properties; @ Bean (name="dataSource") Public DataSource DataSource () { //in accordance with the target data source name and target data source object mapping in the Map Map TargetDataSources=new HashMap<> (a); TargetDataSources. Put (" first ", the properties. The getFirst ()); TargetDataSources. Put (" second ", and the properties. GetSecond ()); TargetDataSources. Put (" third ", and the properties. GetThird ()); //used to AbstractRoutingDataSource object packaging multiple source DynamicDataSource dataSource=new DynamicDataSource (); The dataSource. SetTargetDataSources (targetDataSources); //set the default data source, when get the data source, the use of this configuration is DataSource. SetDefaultTargetDataSource (properties. The getFirst ()); return dataSource; } @ Bean Public PlatformTransactionManager transactionManager () { Return new DataSourceTransactionManager (dataSource ()); } } Public class DynamicDataSourceHolder { /* * * thread local Shared object */ Private static final ThreadLocal THREAD_LOCAL=new ThreadLocal<> (a); Public static void putDataSource (String name) { THREAD_LOCAL. Set (name); } Public static String getDataSource () { Return THREAD_LOCAL. The get (); } Public static void removeDataSource () { THREAD_LOCAL. Remove (); } } Public class DynamicDataSource extends AbstractRoutingDataSource { @ Override Protected Object determineCurrentLookupKey () { //thread from the Shared access to the data source name Return DynamicDataSourceHolder. GetDataSource (); } } If the query can be successful, on behalf of the data source exists, Has not been solved, hope can help have a look at the problem in where CodePudding user response:
The current problem on DynamicDataSourceHolder Run the normal logic, will perform before execution determineCurrentLookupKey putDataSource, But start a new thread and timing task, no execution putDataSource, timing task end or the end of the thread will not perform removeDataSource, The right thing to do Start the new thread scenario The class DataSourceThread extend Thread { Private String dataSource. Private Runnable runable; Public void the run () { DynamicDataSourceHolder. PutDataSource (dataSource); Try { Runable. The run (); } the finally { DynamicDataSourceHolder. RemoveDataSource (); } } } T=new Thread DataSourceThread (" first ", the new Runable () {... }); t.start(); For timing task scenario can only be used in timing task logic to decide what data sources, and DataSourceThread. The logic of the run similar Public void the schedule () { String dataSource=findDataSource ();//a calculation using which the DataSource DynamicDataSourceHolder. PutDataSource (dataSource); Try { RunSelfSchedule (); } the finally { DynamicDataSourceHolder. RemoveDataSource (); } } CodePudding user response:
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull