@@ -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
0 commit comments