List of usage examples for org.hibernate LockOptions UPGRADE
LockOptions UPGRADE
To view the source code for org.hibernate LockOptions UPGRADE.
Click Source Link
From source file:net.derquinse.common.orm.hib.AbstractHibernateSequenceDAO.java
License:Apache License
/** * Gets the next value of a sequence, creating it if it does not exist. * @param id Sequence name./*from ww w . ja va 2 s . c om*/ * @param initial Initial value. * @param increment Increment between values. * @return The next value of the specified sequence. */ public long getNextValue(final String id, final long initial, final long increment) { nonNull(id); final Session session = sessionFactory.getCurrentSession(); @SuppressWarnings("unchecked") T sequence = (T) session.get(persistentClass, id, LockOptions.UPGRADE); if (sequence != null) { return sequence.getNext(); } // Create a new sequence and save it. sequence = create(id, initial, increment); session.save(sequence); // TODO: handle duplicates. return initial; }
From source file:net.derquinse.common.orm.hib.HibernateGeneralDAO.java
License:Apache License
@SuppressWarnings("unchecked") public <T> T findById(Class<T> type, Serializable id, boolean lock) { if (lock) {//from ww w .ja v a 2 s . c o m return (T) getSession().get(type, id, LockOptions.UPGRADE); } return (T) getSession().get(type, id); }
From source file:net.derquinse.common.orm.hib.HibernateGenericDAO.java
License:Apache License
@SuppressWarnings("unchecked") public T findById(ID id, boolean lock) { if (lock) {//from www . j a v a 2 s.c o m return (T) getSession().get(persistentClass, id, LockOptions.UPGRADE); } return (T) getSession().get(persistentClass, id); }
From source file:net.sf.derquinsej.hib3.seq.SequenceDAO.java
License:Apache License
/** * Gets the next value of a sequence. The row is locked in the current transaction. * @param id Sequence name.// w ww . j a v a 2 s . co m * @return The next value of the specified sequence. * @throws SequenceNotFoundException if the sequence is not found. */ public long getNextValue(final String id) throws SequenceNotFoundException { nonNull(id); final Session session = sessionFactory.getCurrentSession(); final Sequence sequence = (Sequence) session.get(Sequence.class, id, LockOptions.UPGRADE); if (sequence == null) { throw new SequenceNotFoundException(id); } final long next = sequence.getNext(); session.update(sequence); return next; }
From source file:net.sf.derquinsej.hib3.seq.SequenceDAO.java
License:Apache License
/** * Gets the next value of a sequence, creating it if it does not exist. * @param id Sequence name.// w ww. j av a 2 s .c o m * @param initial Initial value. * @param increment Increment between values. * @return The next value of the specified sequence. */ public long getNextValue(final String id, final long initial, final long increment) { nonNull(id); final Session session = sessionFactory.getCurrentSession(); Sequence sequence = (Sequence) session.get(Sequence.class, id, LockOptions.UPGRADE); if (sequence != null) { return sequence.getNext(); } // Create a new sequence and save it. sequence = new Sequence(id, initial, increment); session.save(sequence); // TODO: handle duplicates. return initial; }
From source file:ome.server.itests.update.UpdateTest.java
License:Open Source License
@Test(groups = "ticket:5639") public void testCatchDeadlockException() throws Exception { /*/* w w w . j a v a 2 s. co m*/ HibernateInterceptor ht = (HibernateInterceptor) this.applicationContext.getBean("hibernateHandler"); SQLErrorCodeSQLExceptionTranslator trans = (SQLErrorCodeSQLExceptionTranslator) ht.getJdbcExceptionTranslator(); if (trans == null) { trans = new SQLEr ht.setJdbcExceptionTranslator(jdbcExceptionTranslator) } String[] loserCodes = trans.getSqlErrorCodes().getDeadlockLoserCodes(); String[] newLoserCodes; if (loserCodes != null) { newLoserCodes = new String[loserCodes.length+1]; System.arraycopy(loserCodes, 0, newLoserCodes, 0, loserCodes.length); } else { newLoserCodes = new String[1]; } newLoserCodes[newLoserCodes.length-1] = "40P01"; trans.getSqlErrorCodes().setDeadlockLoserCodes(newLoserCodes); */ loginNewUser(); final Image i1 = iUpdate.saveAndReturnObject(new_Image("catchDeadLock1")); final Image i2 = iUpdate.saveAndReturnObject(new_Image("catchDeadLock2")); final CyclicBarrier barrier1 = new CyclicBarrier(2); final CyclicBarrier barrier2 = new CyclicBarrier(2); class T extends Thread { long first, second; String name; Exception e; public T(String name, long first, long second) { super(name); this.name = name; this.first = first; this.second = second; } @Override public void run() { try { executor.execute(loginAop.p, new Executor.SimpleWork(this, name) { @Transactional(readOnly = false) public Object doWork(Session session, ServiceFactory sf) { try { session.get(Image.class, first, LockOptions.UPGRADE); barrier1.await(5, TimeUnit.SECONDS); session.get(Image.class, second, LockOptions.UPGRADE); } catch (RuntimeException rt) { throw rt; } catch (Exception exc) { throw new RuntimeException(e); } finally { try { barrier2.await(5, TimeUnit.SECONDS); } catch (Exception e) { throw new RuntimeException(e); } } return null; } }); } catch (Exception e) { this.e = e; } } } final T t1 = new T("thread1", i1.getId(), i2.getId()); final T t2 = new T("thread2", i2.getId(), i1.getId()); t1.start(); t2.start(); t1.join(); t2.join(); if (t1.e == null) { assertEquals(TryAgain.class, t2.e.getClass()); } else if (t2.e == null) { assertEquals(TryAgain.class, t1.e.getClass()); } else { t1.e.printStackTrace(); t2.e.printStackTrace(); fail("Expected exactly one exception"); } }
From source file:org.apacheextras.camel.component.hibernate.HibernateConsumer.java
License:Open Source License
/** * A strategy method to lock an object with an exclusive lock so that it can * be processed// w ww . ja v a 2 s . co m * * @param entity the entity to be locked * @param session * @return true if the entity was locked */ protected boolean lockEntity(Object entity, Session session) { if (!getEndpoint().isConsumeDelete() || !getEndpoint().isConsumeLockEntity()) { return true; } try { if (LOG.isDebugEnabled()) { LOG.debug("Acquiring exclusive lock on entity: " + entity); } session.buildLockRequest(LockOptions.UPGRADE).setLockMode(LockMode.PESSIMISTIC_WRITE).setTimeOut(60000) .lock(entity); return true; } catch (Exception e) { if (LOG.isDebugEnabled()) { LOG.debug("Failed to achieve lock on entity: " + entity + ". Reason: " + e, e); } return false; } }
From source file:org.bedework.calcore.hibernate.HibSessionImpl.java
License:Apache License
/** * @param o//from w w w .j a v a 2 s . c o m * @throws CalFacadeException */ @Override public void lockUpdate(final Object o) throws CalFacadeException { if (exc != null) { // Didn't hear me last time? throw new CalFacadeException(exc); } try { // sess.lock(o, LockMode.UPGRADE); sess.buildLockRequest(LockOptions.UPGRADE).lock(o); } catch (Throwable t) { handleException(t); } }
From source file:org.cast.cwm.service.ResponseService.java
License:Open Source License
@Override public IModel<BinaryFileData> attachBinaryResponse(IModel<Response> mResponse, FileUpload file) { // If the response is transient, create it // Otherwise, grab the if (mResponse.getObject().isTransient()) { saveResponseWithoutData(mResponse); }/*from w w w . j a va2 s . c o m*/ // Lock the Response LockRequest lock = Databinder.getHibernateSession().buildLockRequest(LockOptions.UPGRADE); lock.lock(mResponse.getObject()); // Upload file BinaryFileData dbFile = new BinaryFileData(file.getClientFileName(), file.getContentType(), file.getBytes()); Databinder.getHibernateSession().save(dbFile); // Associate File with Response mResponse.getObject().getFiles().add(dbFile); // Persist Changes cwmService.flushChanges(); return new HibernateObjectModel<BinaryFileData>(dbFile); }
From source file:org.chenillekit.hibernate.daos.AbstractHibernateDAO.java
License:Apache License
/** * retrieve a entity by his primary key. * * @param id primary key/*from www . j a v a 2s. c o m*/ * @param lock true sets LockMode.UPGRADE * * @return the entity. */ @SuppressWarnings("unchecked") public T doRetrieve(ID id, boolean lock) { T entity; if (id == null) throw new IllegalArgumentException("Parameter id was null!"); if (lock) entity = (T) session.load(getPersistentClass(), id, LockOptions.UPGRADE); else entity = (T) session.load(getPersistentClass(), id); return entity; }