Skip to content

Commit 86ddfb9

Browse files
committed
Extract transaction attributes definition in separate classes
1 parent 5b7f000 commit 86ddfb9

File tree

2 files changed

+37
-20
lines changed

2 files changed

+37
-20
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/JobOperatorFactoryBean.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,7 @@ public void afterPropertiesSet() throws Exception {
102102
this.taskExecutor = new SyncTaskExecutor();
103103
}
104104
if (this.transactionAttributeSource == null) {
105-
this.transactionAttributeSource = new MethodMapTransactionAttributeSource();
106-
DefaultTransactionAttribute transactionAttribute = new DefaultTransactionAttribute();
107-
Method stopMethod = TaskExecutorJobOperator.class.getMethod("stop", JobExecution.class);
108-
Method abandonMethod = TaskExecutorJobOperator.class.getMethod("abandon", JobExecution.class);
109-
Method recoverMethod = TaskExecutorJobOperator.class.getMethod("recover", JobExecution.class);
110-
((MethodMapTransactionAttributeSource) this.transactionAttributeSource).addTransactionalMethod(stopMethod,
111-
transactionAttribute);
112-
((MethodMapTransactionAttributeSource) this.transactionAttributeSource)
113-
.addTransactionalMethod(abandonMethod, transactionAttribute);
114-
((MethodMapTransactionAttributeSource) this.transactionAttributeSource)
115-
.addTransactionalMethod(recoverMethod, transactionAttribute);
105+
this.transactionAttributeSource = new DefaultJobOperatorTransactionAttributeSource();
116106
}
117107
}
118108

@@ -223,4 +213,24 @@ private TaskExecutorJobOperator getTarget() throws Exception {
223213
return taskExecutorJobOperator;
224214
}
225215

216+
private static class DefaultJobOperatorTransactionAttributeSource extends MethodMapTransactionAttributeSource {
217+
218+
public DefaultJobOperatorTransactionAttributeSource() {
219+
DefaultTransactionAttribute transactionAttribute = new DefaultTransactionAttribute();
220+
try {
221+
Method stopMethod = TaskExecutorJobOperator.class.getMethod("stop", JobExecution.class);
222+
Method abandonMethod = TaskExecutorJobOperator.class.getMethod("abandon", JobExecution.class);
223+
Method recoverMethod = TaskExecutorJobOperator.class.getMethod("recover", JobExecution.class);
224+
addTransactionalMethod(stopMethod, transactionAttribute);
225+
addTransactionalMethod(abandonMethod, transactionAttribute);
226+
addTransactionalMethod(recoverMethod, transactionAttribute);
227+
}
228+
catch (NoSuchMethodException e) {
229+
throw new IllegalStateException("Failed to initialize default transaction attributes for JobOperator",
230+
e);
231+
}
232+
}
233+
234+
}
235+
226236
}

spring-batch-core/src/main/java/org/springframework/batch/core/repository/support/AbstractJobRepositoryFactoryBean.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -196,15 +196,8 @@ public void afterPropertiesSet() throws Exception {
196196
jobKeyGenerator = new DefaultJobKeyGenerator();
197197
}
198198
if (this.transactionAttributeSource == null) {
199-
Properties transactionAttributes = new Properties();
200-
transactionAttributes.setProperty("create*",
201-
TRANSACTION_PROPAGATION_PREFIX + Propagation.REQUIRES_NEW + "," + this.isolationLevelForCreate);
202-
transactionAttributes.setProperty("getLastJobExecution*",
203-
TRANSACTION_PROPAGATION_PREFIX + Propagation.REQUIRES_NEW + "," + this.isolationLevelForCreate);
204-
transactionAttributes.setProperty("*", "PROPAGATION_REQUIRED");
205-
this.transactionAttributeSource = new NameMatchTransactionAttributeSource();
206-
((NameMatchTransactionAttributeSource) this.transactionAttributeSource)
207-
.setProperties(transactionAttributes);
199+
this.transactionAttributeSource = new DefaultJobRepositoryTransactionAttributeSource(
200+
this.isolationLevelForCreate);
208201
}
209202
}
210203

@@ -237,4 +230,18 @@ private Object getTarget() throws Exception {
237230
createExecutionContextDao());
238231
}
239232

233+
private static class DefaultJobRepositoryTransactionAttributeSource extends NameMatchTransactionAttributeSource {
234+
235+
public DefaultJobRepositoryTransactionAttributeSource(String isolationLevelForCreate) {
236+
Properties transactionAttributes = new Properties();
237+
transactionAttributes.setProperty("create*",
238+
TRANSACTION_PROPAGATION_PREFIX + Propagation.REQUIRES_NEW + "," + isolationLevelForCreate);
239+
transactionAttributes.setProperty("getLastJobExecution*",
240+
TRANSACTION_PROPAGATION_PREFIX + Propagation.REQUIRES_NEW + "," + isolationLevelForCreate);
241+
transactionAttributes.setProperty("*", "PROPAGATION_REQUIRED");
242+
this.setProperties(transactionAttributes);
243+
}
244+
245+
}
246+
240247
}

0 commit comments

Comments
 (0)