35
35
import org .springframework .jdbc .datasource .DataSourceUtils ;
36
36
import org .springframework .jdbc .support .JdbcUtils ;
37
37
import org .springframework .jdbc .support .MetaDataAccessException ;
38
+ import org .springframework .util .Assert ;
38
39
39
40
/**
40
41
* Subclass of Quartz's {@link JobStoreCMT} class that delegates to a Spring-managed
@@ -88,6 +89,8 @@ public class LocalDataSourceJobStore extends JobStoreCMT {
88
89
89
90
private @ Nullable DataSource dataSource ;
90
91
92
+ private @ Nullable DataSource nonTransactionalDataSource ;
93
+
91
94
92
95
@ Override
93
96
@ SuppressWarnings ("NullAway" ) // Dataflow analysis limitation
@@ -98,19 +101,48 @@ public void initialize(ClassLoadHelper loadHelper, SchedulerSignaler signaler) t
98
101
throw new SchedulerConfigException ("No local DataSource found for configuration - " +
99
102
"'dataSource' property must be set on SchedulerFactoryBean" );
100
103
}
104
+ // Non-transactional DataSource is optional: fall back to default
105
+ // DataSource if not explicitly specified.
106
+ this .nonTransactionalDataSource = SchedulerFactoryBean .getConfigTimeNonTransactionalDataSource ();
101
107
102
- // Configure transactional connection settings for Quartz.
108
+ // Configure connection settings for Quartz.
103
109
setDataSource (TX_DATA_SOURCE_PREFIX + getInstanceName ());
110
+ setNonManagedTXDataSource (NON_TX_DATA_SOURCE_PREFIX + getInstanceName ());
104
111
setDontSetAutoCommitFalse (true );
105
112
113
+ initializeConnectionProvider ();
114
+
115
+ // No, if HSQL is the platform, we really don't want to use locks...
116
+ try {
117
+ String productName = JdbcUtils .extractDatabaseMetaData (this .dataSource ,
118
+ DatabaseMetaData ::getDatabaseProductName );
119
+ productName = JdbcUtils .commonDatabaseName (productName );
120
+ if (productName != null && productName .toLowerCase (Locale .ROOT ).contains ("hsql" )) {
121
+ setUseDBLocks (false );
122
+ setLockHandler (new SimpleSemaphore ());
123
+ }
124
+ }
125
+ catch (MetaDataAccessException ex ) {
126
+ logWarnIfNonZero (1 , "Could not detect database type. Assuming locks can be taken." );
127
+ }
128
+
129
+ super .initialize (loadHelper , signaler );
130
+ }
131
+
132
+ void initializeConnectionProvider () {
133
+ final DataSource dataSourceToUse = this .dataSource ;
134
+ Assert .state (dataSourceToUse != null , "DataSource must not be null" );
135
+ final DataSource nonTxDataSourceToUse =
136
+ (this .nonTransactionalDataSource != null ? this .nonTransactionalDataSource : dataSourceToUse );
137
+
106
138
// Register transactional ConnectionProvider for Quartz.
107
139
DBConnectionManager .getInstance ().addConnectionProvider (
108
140
TX_DATA_SOURCE_PREFIX + getInstanceName (),
109
141
new ConnectionProvider () {
110
142
@ Override
111
143
public Connection getConnection () throws SQLException {
112
144
// Return a transactional Connection, if any.
113
- return DataSourceUtils .doGetConnection (dataSource );
145
+ return DataSourceUtils .doGetConnection (dataSourceToUse );
114
146
}
115
147
@ Override
116
148
public void shutdown () {
@@ -123,14 +155,6 @@ public void initialize() {
123
155
}
124
156
);
125
157
126
- // Non-transactional DataSource is optional: fall back to default
127
- // DataSource if not explicitly specified.
128
- DataSource nonTxDataSource = SchedulerFactoryBean .getConfigTimeNonTransactionalDataSource ();
129
- final DataSource nonTxDataSourceToUse = (nonTxDataSource != null ? nonTxDataSource : this .dataSource );
130
-
131
- // Configure non-transactional connection settings for Quartz.
132
- setNonManagedTXDataSource (NON_TX_DATA_SOURCE_PREFIX + getInstanceName ());
133
-
134
158
// Register non-transactional ConnectionProvider for Quartz.
135
159
DBConnectionManager .getInstance ().addConnectionProvider (
136
160
NON_TX_DATA_SOURCE_PREFIX + getInstanceName (),
@@ -150,23 +174,6 @@ public void initialize() {
150
174
}
151
175
}
152
176
);
153
-
154
- // No, if HSQL is the platform, we really don't want to use locks...
155
- try {
156
- String productName = JdbcUtils .extractDatabaseMetaData (this .dataSource ,
157
- DatabaseMetaData ::getDatabaseProductName );
158
- productName = JdbcUtils .commonDatabaseName (productName );
159
- if (productName != null && productName .toLowerCase (Locale .ROOT ).contains ("hsql" )) {
160
- setUseDBLocks (false );
161
- setLockHandler (new SimpleSemaphore ());
162
- }
163
- }
164
- catch (MetaDataAccessException ex ) {
165
- logWarnIfNonZero (1 , "Could not detect database type. Assuming locks can be taken." );
166
- }
167
-
168
- super .initialize (loadHelper , signaler );
169
-
170
177
}
171
178
172
179
@ Override
0 commit comments