List of usage examples for java.util.concurrent.locks Lock lock
lock
From source file:gridool.db.sql.ParallelSQLExecJob.java
private static void createMergeView(@Nonnull final Connection conn, @Nonnull final String ddl, @Nonnull final ReadWriteLock rwlock) throws GridException { if (LOG.isInfoEnabled()) { LOG.info("Create a merge view: \n" + ddl); }//from w ww.j a v a2s . c om final Lock wlock = rwlock.writeLock(); try { wlock.lock(); JDBCUtils.update(conn, ddl); } catch (SQLException e) { String errmsg = "failed running a reduce query: " + ddl; LOG.error(errmsg, e); try { conn.rollback(); } catch (SQLException rbe) { LOG.warn("Rollback failed", rbe); } throw new GridException(errmsg, e); } finally { wlock.unlock(); } }
From source file:gridool.db.sql.ParallelSQLExecJob.java
private static void runPreparation(@Nonnull final DBAccessor dba, @Nonnull final String mapQuery, @Nonnull final GridNode[] masters, @Nonnull final GridNode localNode, @Nonnull final ReadWriteLock rwlock, @Nonnull final String outputName) throws GridException { final String prepareQuery = constructTaskResultTablesDDL(mapQuery, masters, localNode, outputName); final Connection conn; try {//ww w .jav a2 s . c om conn = dba.getPrimaryDbConnection(); conn.setAutoCommit(false); } catch (SQLException e) { LOG.error("An error caused in the preparation phase", e); throw new GridException(e); } final Lock wlock = rwlock.writeLock(); try { wlock.lock(); JDBCUtils.update(conn, prepareQuery); conn.commit(); } catch (SQLException e) { LOG.error("An error caused in the preparation phase", e); try { conn.rollback(); } catch (SQLException sqle) { LOG.warn("Failed to rollback", sqle); } throw new GridException(e); } finally { wlock.unlock(); JDBCUtils.closeQuietly(conn); } }
From source file:StripedHashSet.java
/** * double the set size// ww w . j av a 2 s . c o m */ @Override public void resize() { int oldCapacity = table.length; for (Lock lock : locks) { lock.lock(); } try { if (oldCapacity != table.length) { return; // someone beat us to it } int newCapacity = 2 * oldCapacity; List<T>[] oldTable = table; table = (List<T>[]) new List[newCapacity]; for (int i = 0; i < newCapacity; i++) table[i] = new ArrayList<T>(); initializeFrom(oldTable); } finally { for (Lock lock : locks) { lock.unlock(); } } }
From source file:gridool.db.sql.ParallelSQLExecJob.java
private static String invokeReduceDDL(final Connection conn, final String reduceQuery, final String outputTableName, final OutputMethod outputMethod, final ReadWriteLock rwlock) throws GridException { final String query; if (outputMethod == OutputMethod.view) { query = "CREATE VIEW \"" + outputTableName + "\" AS (\n" + reduceQuery + ')'; } else if (outputMethod == OutputMethod.table) { query = "CREATE TABLE \"" + outputTableName + "\" AS (\n" + reduceQuery + ") WITH DATA"; } else {//from w ww . j a v a 2s . com throw new IllegalStateException("Unexpected OutputMethod: " + outputMethod); } if (LOG.isInfoEnabled()) { LOG.info("Executing a Reduce SQL query: \n" + query); } final Lock wlock = rwlock.writeLock(); try { wlock.lock(); JDBCUtils.update(conn, query); } catch (SQLException e) { String errmsg = "failed running a reduce query: " + query; LOG.error(errmsg, e); try { conn.rollback(); } catch (SQLException rbe) { LOG.warn("Rollback failed", rbe); } throw new GridException(errmsg, e); } finally { wlock.unlock(); } return outputTableName; }
From source file:de.taimos.daemon.spring.SpringDaemonAdapter.java
/** * @return the Spring context// w ww .ja va2 s. co m */ public final ApplicationContext getContext() { Lock readLock = this.rwLock.readLock(); try { readLock.lock(); return this.context.get(); } finally { readLock.unlock(); } }
From source file:gridool.db.sql.ParallelSQLExecJob.java
private static String invokeStringOutputReduce(final Connection conn, final String reduceQuery, final ReadWriteLock rwlock) throws GridException { final ResultSetHandler rsh = new ResultSetHandler() { public String handle(ResultSet rs) throws SQLException { if (rs.next()) { String firstResult = rs.getString(1); return firstResult; }/* w w w .j a v a2s .c o m*/ return null; } }; if (LOG.isInfoEnabled()) { LOG.info("Executing a Reduce SQL query: \n" + reduceQuery); } final String result; final Lock rlock = rwlock.readLock(); try { rlock.lock(); conn.setReadOnly(true); result = (String) JDBCUtils.query(conn, reduceQuery, rsh); } catch (SQLException e) { String errmsg = "failed running a reduce query: " + reduceQuery; LOG.error(errmsg, e); try { conn.rollback(); } catch (SQLException rbe) { LOG.warn("Rollback failed", rbe); } throw new GridException(errmsg, e); } finally { rlock.unlock(); } return result; }
From source file:cherry.foundation.numbering.SimpleNumberingStore.java
@Override public synchronized long loadAndLock(String numberName) { Lock lock = lockMap.get(numberName); if (lock == null) { lock = new ReentrantLock(true); lockMap.put(numberName, lock);//from w w w . j av a 2s .c o m } lock.lock(); return currentValueMap.get(numberName).longValue(); }
From source file:org.codehaus.wadi.location.partitionmanager.local.BasicLocalPartition.java
public void onMessage(Envelope message, InsertIMToPM request) { Lock readLock = readWriteLock.readLock(); readLock.lock(); try {/*from ww w . java 2 s .c o m*/ LocalPartitionInsertIMToPMAction action = new LocalPartitionInsertIMToPMAction(dispatcher, nameToLocation, log); action.onMessage(message, request); } finally { readLock.unlock(); } }
From source file:org.codehaus.wadi.location.partitionmanager.local.BasicLocalPartition.java
public void onMessage(Envelope message, DeleteIMToPM request) { Lock readLock = readWriteLock.readLock(); readLock.lock(); try {/* ww w .j a va2s . c o m*/ LocalPartitionDeleteIMToPMAction action = new LocalPartitionDeleteIMToPMAction(dispatcher, nameToLocation, log); action.onMessage(message, request); } finally { readLock.unlock(); } }
From source file:org.codehaus.wadi.location.partitionmanager.local.BasicLocalPartition.java
public void onMessage(Envelope message, MoveIMToPM request) { Lock readLock = readWriteLock.readLock(); readLock.lock(); try {/*from ww w . ja v a 2 s.c o m*/ LocalPartitionMoveIMToPMAction action = new LocalPartitionMoveIMToPMAction(dispatcher, nameToLocation, log); action.onMessage(message, request); } finally { readLock.unlock(); } }