1 16 package scriptella.core; 17 18 import scriptella.configuration.Location; 19 import scriptella.configuration.ScriptEl; 20 import scriptella.configuration.ScriptingElement; 21 import scriptella.spi.Connection; 22 23 import java.util.logging.Level ; 24 import java.util.logging.Logger ; 25 26 27 33 public class TxInterceptor extends ElementInterceptor { 34 private static final Logger LOG = Logger.getLogger(TxInterceptor.class.getName()); 35 private Location location; 36 37 public TxInterceptor(ExecutableElement next, ScriptEl scriptEl) { 38 super(next, new TxDecorator(scriptEl)); 39 location=scriptEl.getLocation(); 40 } 41 42 public void execute(final DynamicContext ctx) { 43 final TxDecorator ctxDecorator = (TxDecorator) getCtxDecorator(); 44 ctxDecorator.setContext(ctx); 45 46 try { 47 executeNext(ctxDecorator); 48 } catch (Throwable e) { 49 try { 50 ctxDecorator.c.rollback(); 51 } catch (Exception e1) { 52 LOG.log(Level.WARNING, "Unable to rollback transaction", e1); 53 } 54 LOG.log(Level.INFO, 55 "Script " + location + " failed during invocation in a separate transaction", e); 56 } 57 } 58 59 public static ExecutableElement prepare( 60 final ExecutableElement next, final ScriptEl s) { 61 if (s.isNewTx()) { 62 return new TxInterceptor(next, s); 63 64 } else { 65 return next; 66 } 67 } 68 69 private static class TxDecorator extends DynamicContextDecorator { 70 private Connection c; 71 private String connectionId; 72 73 public TxDecorator(ScriptEl script) { 74 String cid = script.getConnectionId(); 75 if (cid == null) { 77 for (ScriptingElement s = script; (s = s.getParent()) != null;) { 78 cid = s.getConnectionId(); 79 if (cid != null) { 80 break; 81 } 82 } 83 } 84 connectionId=cid; } 86 87 @Override 88 public Connection getConnection() { 89 if (c==null) { 90 c=getGlobalContext().getSession().getConnection(connectionId).newConnection(); 91 } 92 return c; 93 } 94 } 95 } 96 | Popular Tags |