public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { try { sf.getCurrentSession().beginTransaction(); chain.doFilter(request, response); sf.getCurrentSession().getTransaction().commit(); } catch (StaleObjectStateException staleEx) { throw staleEx; } catch (Throwable ex) { // Rollback only ex.printStackTrace(); try { if (sf.getCurrentSession().getTransaction().isActive()) { sf.getCurrentSession().getTransaction().rollback(); } } catch (Throwable rbEx) { System.out.print(rbEx.getStackTrace()); } throw new ServletException(ex); } }
private void handleException(Throwable e) throws VmidcDbConstraintViolationException, VmidcDbConcurrencyException, Exception { if (e instanceof SslCertificatesExtendedException) { throw (SslCertificatesExtendedException) e; } else if (e instanceof VmidcException) { log.warn("Service request failed (logically): " + e.getMessage()); } else { log.error("Service request failed (unexpectedly): " + e.getMessage(), e); } if (e instanceof ConstraintViolationException) { log.error("Got database constraint violation exception", e); throw new VmidcDbConstraintViolationException("Database Constraint Violation Exception."); } else if (e instanceof StaleObjectStateException) { log.error("Got database concurrency exception", e); throw new VmidcDbConcurrencyException("Database Concurrency Exception."); } else if (e instanceof Exception) { throw (Exception) e; } throw new Exception("Exception or error executing service call: " + e.getMessage(), e); }
private Object[] getDatabaseSnapshot(SessionImplementor session, EntityPersister persister, Serializable id) { if ( persister.isSelectBeforeUpdateRequired() ) { Object[] snapshot = session.getPersistenceContext() .getDatabaseSnapshot( id, persister ); if ( snapshot == null ) { //do we even really need this? the update will fail anyway.... if ( session.getFactory().getStatistics().isStatisticsEnabled() ) { session.getFactory().getStatisticsImplementor() .optimisticFailure( persister.getEntityName() ); } throw new StaleObjectStateException( persister.getEntityName(), id ); } return snapshot; } // TODO: optimize away this lookup for entities w/o unsaved-value="undefined" final EntityKey entityKey = session.generateEntityKey( id, persister ); return session.getPersistenceContext().getCachedDatabaseSnapshot( entityKey ); }
@Override public void execute() { Transaction tx = session.beginTransaction(); try{ if (primerak != null) session.update(primerak); if (lending != null) session.update(lending); tx.commit(); if (lending != null){ log.info("discharging book successfully: "+ lending.getCtlgNo()); } else { log.info("discharging book successfully: "); } success = true; }catch (StaleObjectStateException e){ tx.rollback(); if (lending != null){ log.info("discharging book failed: "+ lending.getCtlgNo()); } else { log.info("discharging book failed: "); } log.error(e); message = e.getMessage(); } }
private Object[] getDatabaseSnapshot(SessionImplementor session, EntityPersister persister, Serializable id) { if ( persister.isSelectBeforeUpdateRequired() ) { Object[] snapshot = session.getPersistenceContext() .getDatabaseSnapshot(id, persister); if (snapshot==null) { //do we even really need this? the update will fail anyway.... if ( session.getFactory().getStatistics().isStatisticsEnabled() ) { session.getFactory().getStatisticsImplementor() .optimisticFailure( persister.getEntityName() ); } throw new StaleObjectStateException( persister.getEntityName(), id ); } else { return snapshot; } } else { //TODO: optimize away this lookup for entities w/o unsaved-value="undefined" EntityKey entityKey = new EntityKey( id, persister, session.getEntityMode() ); return session.getPersistenceContext() .getCachedDatabaseSnapshot( entityKey ); } }
/** * (non-Javadoc) * * @see architecture.hibernate.DataAccess#save(java.lang.Object) * also starts a transaction, if none is active */ @Override public boolean save(T obj) { Session s = dbAccess.getActiveSession(); try { s.saveOrUpdate(obj); return true; } catch (StaleObjectStateException stex) { s.refresh(obj); return false; } catch (RuntimeException rex) { Transaction t = s.getTransaction(); if (t != null) { t.rollback(); } s.close(); throw rex; } }
@Override public void run() { try { doInJPA(entityManager -> { try { Post post = entityManager.find(Post.class, 1L); loadPostLatch.countDown(); loadPostLatch.await(); post.setTitle("JPA"); } catch (InterruptedException e) { throw new IllegalStateException(e); } }); } catch (StaleObjectStateException expected) { LOGGER.info("Alice: Optimistic locking failure", expected); } aliceLatch.countDown(); }
@Override public void run() { try { doInJPA(entityManager -> { try { Post post = entityManager.find(Post.class, 1L); loadPostLatch.countDown(); loadPostLatch.await(); aliceLatch.await(); post.incrementLikes(); } catch (InterruptedException e) { throw new IllegalStateException(e); } }); } catch (StaleObjectStateException expected) { LOGGER.info("Bob: Optimistic locking failure", expected); } }
@Override public void run() { try { doInJPA(entityManager -> { try { Post post = entityManager.find(Post.class, 1L); loadPostLatch.countDown(); loadPostLatch.await(); aliceLatch.await(); post.setViews(15); } catch (InterruptedException e) { throw new IllegalStateException(e); } }); } catch (StaleObjectStateException expected) { LOGGER.info("Carol: Optimistic locking failure", expected); } }
private void testPessimisticLocking(LockRequestCallable primaryLockRequestCallable, LockRequestCallable secondaryLockRequestCallable) { doInJPA(entityManager -> { try { Session session = entityManager.unwrap(Session.class); Post post = entityManager.find(Post.class, 1L); primaryLockRequestCallable.lock(session, post); executeAsync( () -> { doInJPA(_entityManager -> { Session _session = _entityManager.unwrap(Session.class); Post _post = _entityManager.find(Post.class, 1L); secondaryLockRequestCallable.lock(_session, _post); }); }, endLatch::countDown ); sleep(WAIT_MILLIS); } catch (StaleObjectStateException e) { LOGGER.info("Optimistic locking failure: ", e); } }); awaitOnLatch(endLatch); }
@Override public void run() { try { doInJPA(entityManager -> { try { entityManager.unwrap(Session.class).doWork(connection -> { assertEquals(Connection.TRANSACTION_REPEATABLE_READ, connection.getTransactionIsolation()); }); Post post = entityManager.find(Post.class, 1L); loadPostLatch.countDown(); loadPostLatch.await(); post.setTitle("JPA"); } catch (InterruptedException e) { throw new IllegalStateException(e); } }); } catch (StaleObjectStateException expected) { LOGGER.info("Alice: Optimistic locking failure", expected); } catch (Exception unexpected) { LOGGER.info("Alice: Optimistic locking failure due to MVCC", unexpected); } aliceLatch.countDown(); }
@Override public void run() { try { doInJPA(entityManager -> { try { entityManager.unwrap(Session.class).doWork(connection -> { assertEquals(Connection.TRANSACTION_REPEATABLE_READ, connection.getTransactionIsolation()); }); Post post = entityManager.find(Post.class, 1L); loadPostLatch.countDown(); loadPostLatch.await(); aliceLatch.await(); post.incrementLikes(); } catch (InterruptedException e) { throw new IllegalStateException(e); } }); } catch (StaleObjectStateException expected) { LOGGER.info("Bob: Optimistic locking failure", expected); } catch (Exception unexpected) { LOGGER.info("Bob: Optimistic locking failure due to MVCC", unexpected); } }
@Test public void testConcurrentPessimisticForceIncrementLockingFailFast() { try { doInJPA(entityManager -> { Repository repository = entityManager.find(Repository.class, 1L); executeSync(() -> { doInJPA(_entityManager -> { Repository _repository = _entityManager.find(Repository.class, 1L, LockModeType.PESSIMISTIC_FORCE_INCREMENT); Commit _commit = new Commit(_repository); _commit.getChanges().add(new Change("Intro.md", "0a1,2...")); _entityManager.persist(_commit); }); }); entityManager.lock(repository, LockModeType.PESSIMISTIC_FORCE_INCREMENT); fail("Should have thrown StaleObjectStateException!"); }); } catch (OptimisticLockException expected) { assertEquals(StaleObjectStateException.class, expected.getCause().getClass()); LOGGER.info("Failure: ", expected); } }
/** */ @Override public void deleteBusinessGroup(BusinessGroup businessGroupTodelete) { try { // Delete the group object itself on the database db.deleteObject(businessGroupTodelete); } catch (final DBRuntimeException dbre) { final Throwable th = dbre.getCause(); if ((th instanceof StaleObjectStateException) && (th.getMessage().startsWith("Row was updated or deleted by another transaction"))) { // known issue OLAT-3654 log.info("Group was deleted by another user in the meantime. Known issue OLAT-3654"); throw new KnownIssueException("Group was deleted by another user in the meantime", 3654); } else { throw dbre; } } }
/** */ @Override public void deleteBusinessGroup(BusinessGroup businessGroupTodelete) { try { // Delete the group object itself on the database db.deleteObject(businessGroupTodelete); } catch (final DBRuntimeException dbre) { final Throwable th = dbre.getCause(); if ((th instanceof StaleObjectStateException) && (th.getMessage().startsWith("Row was updated or deleted by another transaction"))) { // known issue OLAT-3654 log.info("Group was deleted by another user in the meantime. Known issue OLAT-3654"); throw new KnownIssueException("Group was deleted by another user in the meantime", 3654, dbre); } else { throw dbre; } } }
@Override public void run() { try { doInTransaction(session -> { try { Product product = (Product) session.get(Product.class, 1L); loadProductsLatch.countDown(); loadProductsLatch.await(); product.setQuantity(6L); } catch (InterruptedException e) { throw new IllegalStateException(e); } }); } catch (StaleObjectStateException expected) { LOGGER.info("Alice: Optimistic locking failure", expected); } aliceLatch.countDown(); }
@Override public void run() { try { doInTransaction(session -> { try { Product product = (Product) session.get(Product.class, 1L); loadProductsLatch.countDown(); loadProductsLatch.await(); aliceLatch.await(); product.incrementLikes(); } catch (InterruptedException e) { throw new IllegalStateException(e); } }); } catch (StaleObjectStateException expected) { LOGGER.info("Bob: Optimistic locking failure", expected); } }
@Override public void run() { try { doInTransaction(session -> { try { Product product = (Product) session.get(Product.class, 1L); loadProductsLatch.countDown(); loadProductsLatch.await(); aliceLatch.await(); product.setDescription("Plasma HDTV"); } catch (InterruptedException e) { throw new IllegalStateException(e); } }); } catch (StaleObjectStateException expected) { LOGGER.info("Vlad: Optimistic locking failure", expected); } }
private void testPessimisticLocking(ProductLockRequestCallable primaryLockRequestCallable, ProductLockRequestCallable secondaryLockRequestCallable) { doInTransaction(session -> { try { Product product = (Product) session.get(Product.class, 1L); primaryLockRequestCallable.lock(session, product); executeAsync( () -> { doInTransaction(_session -> { Product _product = (Product) _session.get(Product.class, 1L); secondaryLockRequestCallable.lock(_session, _product); }); }, endLatch::countDown ); sleep(WAIT_MILLIS); } catch (StaleObjectStateException e) { LOGGER.info("Optimistic locking failure: ", e); } }); awaitOnLatch(endLatch); }
@Test public void testConcurrentPessimisticForceIncrementLockingFailFast() throws InterruptedException { LOGGER.info("Test Concurrent PESSIMISTIC_FORCE_INCREMENT Lock Mode fail fast"); doInTransaction(session -> { try { Repository repository = (Repository) session.get(Repository.class, 1L); executeSync(() -> { doInTransaction(_session -> { Repository _repository = (Repository) _session.get(Repository.class, 1L); _session.buildLockRequest(new LockOptions(LockMode.PESSIMISTIC_FORCE_INCREMENT)).lock(_repository); Commit _commit = new Commit(_repository); _commit.getChanges().add(new Change("index.html", "0a1,2...")); _session.persist(_commit); _session.flush(); }); }); session.buildLockRequest(new LockOptions(LockMode.PESSIMISTIC_FORCE_INCREMENT)).lock(repository); fail("Should have thrown StaleObjectStateException!"); } catch (StaleObjectStateException expected) { LOGGER.info("Failure: ", expected); } }); }
public void lock(Serializable id, Object version, Object object, int timeout, SessionImplementor session) throws StaleObjectStateException, JDBCException { if ( getLockMode().greaterThan( LockMode.READ ) ) { log.warn( "HSQLDB supports only READ_UNCOMMITTED isolation" ); } super.lock( id, version, object, timeout, session ); }
public void lock(Serializable id, Object version, Object object, SessionImplementor session) throws StaleObjectStateException, JDBCException { if ( getLockMode().greaterThan( LockMode.READ ) ) { log.warn( "HSQLDB supports only READ_UNCOMMITTED isolation" ); } super.lock( id, version, object, session ); }
/** * Check the version of the object in the <tt>ResultSet</tt> against * the object version in the session cache, throwing an exception * if the version numbers are different */ private void checkVersion( final int i, final Loadable persister, final Serializable id, final Object entity, final ResultSet rs, final SessionImplementor session) throws HibernateException, SQLException { Object version = session.getPersistenceContext().getEntry( entity ).getVersion(); if ( version != null ) { //null version means the object is in the process of being loaded somewhere else in the ResultSet VersionType versionType = persister.getVersionType(); Object currentVersion = versionType.nullSafeGet( rs, getEntityAliases()[i].getSuffixedVersionAliases(), session, null ); if ( !versionType.isEqual(version, currentVersion) ) { if ( session.getFactory().getStatistics().isStatisticsEnabled() ) { session.getFactory().getStatisticsImplementor() .optimisticFailure( persister.getEntityName() ); } throw new StaleObjectStateException( persister.getEntityName(), id ); } } }
private void checkVersion( SessionImplementor session, ResultSet resultSet, EntityPersister persister, EntityAliases entityAliases, EntityKey entityKey, Object entityInstance) { final Object version = session.getPersistenceContext().getEntry( entityInstance ).getVersion(); if ( version != null ) { //null version means the object is in the process of being loaded somewhere else in the ResultSet VersionType versionType = persister.getVersionType(); final Object currentVersion; try { currentVersion = versionType.nullSafeGet( resultSet, entityAliases.getSuffixedVersionAliases(), session, null ); } catch (SQLException e) { throw session.getFactory().getJdbcServices().getSqlExceptionHelper().convert( e, "Could not read version value from result set" ); } if ( !versionType.isEqual( version, currentVersion ) ) { if ( session.getFactory().getStatistics().isStatisticsEnabled() ) { session.getFactory().getStatisticsImplementor().optimisticFailure( persister.getEntityName() ); } throw new StaleObjectStateException( persister.getEntityName(), entityKey.getIdentifier() ); } } }
public void lock(Serializable id, Object version, Object object, int timeout, SessionImplementor session) throws StaleObjectStateException, JDBCException { if ( getLockMode().greaterThan( LockMode.READ ) ) { LOG.hsqldbSupportsOnlyReadCommittedIsolation(); } super.lock( id, version, object, timeout, session ); }
public void updateGradebook(final Gradebook gradebook) throws StaleObjectModificationException { HibernateCallback hc = session -> { // Get the gradebook and selected mapping from persistence Gradebook gradebookFromPersistence = (Gradebook)session.load(gradebook.getClass(), gradebook.getId()); GradeMapping mappingFromPersistence = gradebookFromPersistence.getSelectedGradeMapping(); // If the mapping has changed, and there are explicitly entered // course grade records, disallow this update. if (!mappingFromPersistence.getId().equals(gradebook.getSelectedGradeMapping().getId())) { if(isExplicitlyEnteredCourseGradeRecords(gradebook.getId())) { throw new IllegalStateException("Selected grade mapping can not be changed, since explicit course grades exist."); } } // Evict the persisted objects from the session and update the gradebook // so the new grade mapping is used in the sort column update //session.evict(mappingFromPersistence); for(Iterator iter = gradebookFromPersistence.getGradeMappings().iterator(); iter.hasNext();) { session.evict(iter.next()); } session.evict(gradebookFromPersistence); try { session.update(gradebook); session.flush(); } catch (StaleObjectStateException e) { throw new StaleObjectModificationException(e); } return null; }; getHibernateTemplate().execute(hc); }
@Test(expected = StaleObjectStateException.class) public void testUpdateLock() throws SQLException { Person person = new Person(); person.setId(1l); person.setName("test name 1"); person.setVersion(0); IPersistentConfiguration configuration = context.getInitializedData(IPersistentConfigurationInitializer.KEY); SessionFactory sessionFactory = configuration.getRealConfiguration(); sessionFactory.getCurrentSession().beginTransaction(); descriptor.getSaver().update(person); sessionFactory.getCurrentSession().getTransaction().commit(); }
@Test(expected = StaleObjectStateException.class) public void testUpdateLock() throws SQLException { Person person = new Person(); person.setId(1l); person.setName("test name 1"); person.setVts(new Timestamp(ts.getTime() + 1)); IPersistentConfiguration configuration = context.getInitializedData(IPersistentConfigurationInitializer.KEY); SessionFactory sessionFactory = configuration.getRealConfiguration(); sessionFactory.getCurrentSession().beginTransaction(); descriptor.getSaver().update(person); sessionFactory.getCurrentSession().getTransaction().commit(); }