List of usage examples for org.hibernate LockOptions LockOptions
public LockOptions(LockMode lockMode)
From source file:org.springframework.orm.hibernate4.HibernateTemplate.java
License:Apache License
@Override public void delete(final Object entity, final LockMode lockMode) throws DataAccessException { executeWithNativeSession(new HibernateCallback<Object>() { @Override/*from ww w .j a v a 2 s. c om*/ public Object doInHibernate(Session session) throws HibernateException { checkWriteOperationAllowed(session); if (lockMode != null) { session.buildLockRequest(new LockOptions(lockMode)).lock(entity); } session.delete(entity); return null; } }); }
From source file:org.springframework.orm.hibernate4.HibernateTemplate.java
License:Apache License
@Override public void delete(final String entityName, final Object entity, final LockMode lockMode) throws DataAccessException { executeWithNativeSession(new HibernateCallback<Object>() { @Override/* w w w.j ava 2s . co m*/ public Object doInHibernate(Session session) throws HibernateException { checkWriteOperationAllowed(session); if (lockMode != null) { session.buildLockRequest(new LockOptions(lockMode)).lock(entityName, entity); } session.delete(entityName, entity); return null; } }); }
From source file:org.springframework.orm.hibernate5.HibernateTemplate.java
License:Apache License
@Override public <T> T get(final Class<T> entityClass, final Serializable id, final LockMode lockMode) throws DataAccessException { return executeWithNativeSession(new HibernateCallback<T>() { @Override//from www. j a v a 2s . c o m public T doInHibernate(Session session) throws HibernateException { if (lockMode != null) { return session.get(entityClass, id, new LockOptions(lockMode)); } else { return session.get(entityClass, id); } } }); }
From source file:org.springframework.orm.hibernate5.HibernateTemplate.java
License:Apache License
@Override public <T> T load(final Class<T> entityClass, final Serializable id, final LockMode lockMode) throws DataAccessException { return executeWithNativeSession(new HibernateCallback<T>() { @Override//from ww w .ja v a 2 s. com public T doInHibernate(Session session) throws HibernateException { if (lockMode != null) { return session.load(entityClass, id, new LockOptions(lockMode)); } else { return session.load(entityClass, id); } } }); }
From source file:org.vpac.ndg.storage.dao.TimeSliceLockDaoImpl.java
License:Open Source License
@Transactional(readOnly = false, isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED) TimeSliceLock tryReadLock(String timeSliceId, String processId, String operation, String user) throws IllegalMonitorStateException { // First lock the timeslice row. Set scope to prevent cascading. TimeSlice ts = (TimeSlice) getSession().load(TimeSlice.class, timeSliceId, new LockOptions(LockMode.PESSIMISTIC_WRITE)); // Check status. Can't lock if there is a write lock, but can if there // are read locks. if (ts.getLockCount() > 0 && ts.getLockMode() == 'w') { // Throw unchecked exception to force rollback. // http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/transaction.html#transaction-declarative-rolling-back log.debug("{} is write-locked", ts); throw new IllegalMonitorStateException("Already locked."); }//from ww w .j a v a 2 s . c o m log.trace("Incrementing read lock for {}; was {}", ts.getLockCount()); ts.setLockCount(ts.getLockCount() + 1); ts.setLockMode('r'); getSession().saveOrUpdate(ts); // Now that the lock has been obtained, create some metadata to allow // others to trace the lock. This is very important: without this, it's // impossible to distinguish between a valid lock and one that belonged // to a machine that has crashed. TimeSliceLock tslock = new TimeSliceLock(); tslock.setTimesliceId(ts.getId()); tslock.setProcessId(processId); tslock.setOperation(operation); tslock.setUser(user); tslock.setState(RunningTaskState.RUNNING); create(tslock); return tslock; }
From source file:org.vpac.ndg.storage.dao.TimeSliceLockDaoImpl.java
License:Open Source License
@Transactional(readOnly = false, isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED) TimeSliceLock tryWriteLock(String timeSliceId, String processId, String operation, String user) throws IllegalMonitorStateException { // First lock the timeslice row. Set scope to prevent cascading. TimeSlice ts = (TimeSlice) getSession().load(TimeSlice.class, timeSliceId, new LockOptions(LockMode.PESSIMISTIC_WRITE)); // Check status. We can't lock if there are *any* other locks held - // read or write. if (ts.getLockCount() > 0) { // Throw unchecked exception to force rollback. // http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/transaction.html#transaction-declarative-rolling-back log.debug("{} is already locked (count: {})", ts, ts.getLockCount()); throw new IllegalMonitorStateException("Already locked."); }//w w w .ja v a2s .c o m log.trace("Incrementing write lock for {}; was {}", ts.getLockCount()); ts.setLockCount(ts.getLockCount() + 1); ts.setLockMode('w'); getSession().saveOrUpdate(ts); // Now that the lock has been obtained, create some metadata to allow // others to trace the lock. This is very important: without this, it's // impossible to distinguish between a valid lock and one that belonged // to a machine that has crashed. TimeSliceLock tslock = new TimeSliceLock(); tslock.setTimesliceId(ts.getId()); tslock.setProcessId(processId); tslock.setOperation(operation); tslock.setUser(user); tslock.setState(RunningTaskState.RUNNING); create(tslock); return tslock; }
From source file:org.vpac.ndg.storage.dao.TimeSliceLockDaoImpl.java
License:Open Source License
@Override @Transactional(readOnly = false, isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED) public void unlock(TimeSliceLock lock) { // First lock the timeslice row. Set scope to prevent cascading. TimeSlice ts = (TimeSlice) getSession().load(TimeSlice.class, lock.getTimesliceId(), new LockOptions(LockMode.PESSIMISTIC_WRITE)); // Check status. if (ts.getLockCount() <= 0) { throw new IllegalMonitorStateException(String.format("Time slice %s is not locked.", ts)); }/* w ww. ja va 2s. c o m*/ log.trace("Decrementing read lock for {}", ts); ts.setLockCount(ts.getLockCount() - 1); getSession().saveOrUpdate(ts); delete(lock); }
From source file:org.vpac.ndg.storage.dao.TimeSliceLockDaoImpl.java
License:Open Source License
@Override @Transactional(readOnly = false, isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED) public TimeSliceLock adoptOne(String adopterProcessId) throws InterruptedException { List<TimeSliceLock> lockList = listOrphaned(); if (lockList.size() == 0) return null; // Lock the row of the token and try to update it. TimeSliceLock lockToken = lockList.get(0); lockToken = (TimeSliceLock) getSession().load(TimeSliceLock.class, lockToken.getId(), new LockOptions(LockMode.PESSIMISTIC_WRITE)); Process proc = processDao.retrieve(lockToken.getProcessId()); if (proc != null) { throw new InterruptedException( String.format("Lock %s was " + "apparently taken by another process.", lockToken.getId())); }/*from w w w .jav a 2s . co m*/ lockToken.setProcessId(adopterProcessId); update(lockToken); return lockToken; }
From source file:org.webical.dao.hibernateImpl.CalendarDaoHibernateImpl.java
License:Open Source License
@Transaction(readOnly = false) public void removeCalendar(Calendar calendar) throws DaoException { try {/*from ww w . j a v a2 s .com*/ log.info("Deleting calendar " + calendar.getName()); getSession().buildLockRequest(new LockOptions(LockMode.NONE)).lock(calendar); //Cascade events in the cache EventDao eventDao = DaoFactory.getInstance().getEventDaoForCalendar(calendar); if (eventDao != null) { eventDao.removeAllEventsForCalendar(calendar); } delete(calendar); } catch (Exception e) { log.error(e, e); throw new DaoException("Could not remove Calendar", e); } }
From source file:org.webical.dao.hibernateImpl.EventDaoWebDavHibernateBufferedImpl.java
License:Open Source License
@Transaction(readOnly = false) public void removeEvent(Event event) throws DaoException { if (event == null) return;//from ww w .ja va 2 s. co m try { if (log.isDebugEnabled()) log.debug("removeEvent: " + event.toString()); // Remove the event from the session getSession().buildLockRequest(new LockOptions(LockMode.NONE)).lock(event); delete(event); // First refresh the calendar from remote List<Event> events = refreshCalendarEvents(event.getCalendar()); // Remove the event (if still exists) Event removedEvent = null; for (Event e : events) { if (e.getUid().equals(event.getUid())) { removedEvent = e; } } // 2x ? if (removedEvent != null) { getSession().buildLockRequest(new LockOptions(LockMode.NONE)).lock(removedEvent); delete(removedEvent); //Synchronize the removed event //Create a new WebDavCalendarSynchronisation util WebDavCalendarSynchronisation calendarSynchronisation = new WebDavCalendarSynchronisation(); //Sync to the remote ics file calendarSynchronisation.writeToRemoteCalendarFile(removedEvent.getCalendar(), getAllEvents(removedEvent.getCalendar())); } } catch (Exception e) { log.error(e, e); throw new DaoException("Could not remove Event", e); } }