Skip to content

Commit 34b7b07

Browse files
committed
fix GroovyScriptRunningContext
1 parent 12d2c87 commit 34b7b07

3 files changed

Lines changed: 58 additions & 30 deletions

File tree

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.codingapi.springboot.flow.script;
22

3-
import com.codingapi.springboot.framework.script.GroovyScriptRunningContext;
3+
import com.codingapi.springboot.framework.script.GroovyScriptRunner;
44
import lombok.Getter;
55

66
public class GroovyShellContext {
@@ -11,25 +11,25 @@ public class GroovyShellContext {
1111
@Getter
1212
private final static GroovyShellContext instance = new GroovyShellContext();
1313

14+
private final GroovyScriptRunner scriptRunner;
15+
1416

1517
private GroovyShellContext() {
16-
if(GroovyScriptRunningContext.getInstance().getMaxCacheSize()!=MAX_CACHE_SIZE) {
17-
GroovyScriptRunningContext.getInstance().setMaxCacheSize(MAX_CACHE_SIZE);
18-
}
18+
scriptRunner = new GroovyScriptRunner(MAX_CACHE_SIZE);
1919
}
2020

2121

2222
public <T> T run(String script, Class<T> returnType, Object... args) {
23-
return GroovyScriptRunningContext.getInstance().run("run", script, returnType, args);
23+
return scriptRunner.run("run", script, returnType, args);
2424
}
2525

2626

27-
public int size(){
28-
return GroovyScriptRunningContext.getInstance().cacheSize();
27+
public int size() {
28+
return scriptRunner.cacheSize();
2929
}
3030

31-
public void clear(){
32-
GroovyScriptRunningContext.getInstance().clearCache();
31+
public void clear() {
32+
scriptRunner.clearCache();
3333
}
3434

3535
}

‎springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunner.java‎

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,26 @@ public class GroovyScriptRunner {
3030
private final int maxCacheSize;
3131

3232
@Getter
33-
private final boolean readOnly;
33+
private final TransactionMode transactionMode;
3434

35-
public GroovyScriptRunner(int maxCacheSize){
36-
this(maxCacheSize,false);
35+
36+
public static enum TransactionMode {
37+
// 默认不处理
38+
DEFAULT,
39+
// 事务提交模式
40+
COMMIT,
41+
// 事务只读模式
42+
READONLY
43+
}
44+
45+
46+
public GroovyScriptRunner(int maxCacheSize) {
47+
this(maxCacheSize, TransactionMode.DEFAULT);
3748
}
3849

39-
public GroovyScriptRunner(int maxCacheSize, boolean readOnly) {
50+
public GroovyScriptRunner(int maxCacheSize, TransactionMode transactionMode) {
4051
this.maxCacheSize = maxCacheSize;
41-
this.readOnly = readOnly;
52+
this.transactionMode = transactionMode;
4253
GroovyClassLoader groovyClassLoader = new GroovyClassLoader(getClass().getClassLoader());
4354
this.shell = new GroovyShell(groovyClassLoader);
4455
this.cache = new LinkedHashMap<>(16, 0.75F, true) {
@@ -117,27 +128,44 @@ public <T> T run(String method, String script, Class<T> returnType, List<GroovyB
117128
}
118129
}
119130

120-
PlatformTransactionManager transactionManager = TransactionManagerContext.getInstance().getPlatformTransactionManager();
121-
if (transactionManager != null) {
122-
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
123-
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
124-
if (readOnly) {
131+
// 事务制只读模式
132+
if (transactionMode == TransactionMode.READONLY) {
133+
PlatformTransactionManager transactionManager = TransactionManagerContext.getInstance().getPlatformTransactionManager();
134+
if (transactionManager != null) {
135+
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
136+
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
125137
def.setReadOnly(true);
126-
}
127-
TransactionStatus transactionStatus = transactionManager.getTransaction(def);
128-
try {
129-
T result = (T) runtime.invokeMethod(method, args);
130-
if (readOnly) {
138+
TransactionStatus transactionStatus = transactionManager.getTransaction(def);
139+
try {
140+
T result = (T) runtime.invokeMethod(method, args);
141+
transactionManager.rollback(transactionStatus);
142+
return result;
143+
} catch (Exception e) {
131144
transactionManager.rollback(transactionStatus);
132-
} else {
145+
throw e;
146+
}
147+
}
148+
}
149+
150+
// 事务制提交模式
151+
if (transactionMode == TransactionMode.COMMIT) {
152+
PlatformTransactionManager transactionManager = TransactionManagerContext.getInstance().getPlatformTransactionManager();
153+
if (transactionManager != null) {
154+
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
155+
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
156+
TransactionStatus transactionStatus = transactionManager.getTransaction(def);
157+
try {
158+
T result = (T) runtime.invokeMethod(method, args);
133159
transactionManager.commit(transactionStatus);
160+
return result;
161+
} catch (Exception e) {
162+
transactionManager.rollback(transactionStatus);
163+
throw e;
134164
}
135-
return result;
136-
} catch (Exception e) {
137-
transactionManager.rollback(transactionStatus);
138-
throw e;
139165
}
140166
}
167+
168+
// 默认处理模式
141169
return (T) runtime.invokeMethod(method, args);
142170
}
143171

‎springboot-starter/src/main/java/com/codingapi/springboot/framework/script/GroovyScriptRunningContext.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class GroovyScriptRunningContext {
1919
private static GroovyScriptRunningContext instance = new GroovyScriptRunningContext(MAX_CACHE_SIZE);
2020

2121
private GroovyScriptRunningContext(int maxCacheSize) {
22-
this.scriptRunner = new GroovyScriptRunner(maxCacheSize, false);
22+
this.scriptRunner = new GroovyScriptRunner(maxCacheSize);
2323
}
2424

2525
/**

0 commit comments

Comments
 (0)