Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package de.zalando.sprocwrapper.proxy.executors;
  
  import java.sql.ResultSet;
  import java.sql.Statement;
  
  
 
This Executor wraps stored procedure calls that use advisory locks and / or need different statement timeouts set.

Author(s):
jmussler
 
 public class ExecutorWrapper implements Executor {
 
     private final Executor executor;
     private final long timeoutInMilliSeconds;
     private final AdvisoryLock lock;
 
     private static final Logger LOG = LoggerFactory.getLogger(ExecutorWrapper.class);
 
     public ExecutorWrapper(final Executor efinal long tfinal AdvisoryLock a) {
          = e;
          = t;
          = a;
     }
 
     private void setTimeout(final Connection connthrows SQLException {
         if ( <= 0) {
             return;
         }
 
         .debug("Setting statement timeout {}");
 
         final Statement st = conn.createStatement();
         st.execute("SET application_name TO 'timeout:" +  + "'");
         st.execute("SET statement_timeout TO " + );
         st.close();
     }
 
     private void resetTimeout(final Connection connthrows SQLException {
         if ( <= 0) {
             return;
         }
 
         .debug("Resetting statement timeout");
 
         final Statement st = conn.createStatement();
         st.execute("RESET statement_timeout");
         st.execute("RESET application_name");
         st.close();
     }
 
     private boolean lockAdvisoryLock(final Connection connthrows SQLException {
         if ( == .) {
             return true;
         }
 
         final Statement st = conn.createStatement();
         final ResultSet rs = st.executeQuery("SELECT pg_advisory_lock(" + .getSprocId() + ") AS \"" + .name()
                     + "\";");
 
         boolean b = false;
         if (rs.next()) {
             b = true;
         }
 
         rs.close();
         st.close();
         return b;
     }
 
     private boolean unlockAdvisoryLock(final Connection connthrows SQLException {
         if ( == .) {
             return true;
         }
 
         final Statement st = conn.createStatement();
         final ResultSet rs = st.executeQuery("SELECT pg_advisory_unlock(" + .getSprocId() + ")");
         boolean b = false;
         if (rs.next()) {
             b = rs.getBoolean(1);
         }
 
         rs.close();
         st.close();
         return b;
     }
 
     @Override
     public Object executeSProc(final DataSource dsfinal String sqlfinal Object[] argsfinal int[] types,
            final InvocationContext invocationContextfinal Class<?> returnType) {
        SameConnectionDatasource sameConnDs = null;
        try {
            sameConnDs = new SameConnectionDatasource(ds.getConnection());
            setTimeout(sameConnDs.getConnection());
            if (!lockAdvisoryLock(sameConnDs.getConnection())) {
                throw new RuntimeException("Could not acquire AdvisoryLock " + .name());
            }
            return .executeSProc(sameConnDssqlargstypesinvocationContextreturnType);
        } catch (final SQLException e) {
            throw new RuntimeException("SQL Exception in execute sproc: " + sqle);
        } finally {
            if (sameConnDs != null) {
                try {
                    if ( > 0) {
                        try {
                            resetTimeout(sameConnDs.getConnection());
                        } catch (final SQLException ex) {
                            .error("Exception in reseting statement timeout!"ex);
                        }
                    }
                    // unlock in all cases, locks not owned by this session cannot be unlocked
                    if ( != .) {
                        try {
                            unlockAdvisoryLock(sameConnDs.getConnection());
                        } catch (final SQLException ex) {
                            .error("Exception in reseting advisory lock!"ex);
                        }
                    }
                } finally {
                    try {
                        sameConnDs.close();
                    } catch (final SQLException ex) {
                        .error("Exception in closing underlying connection!"ex);
                    }
                }
            }
        }
    }
New to GrepCode? Check out our FAQ X