synchronized (this) { CtlSchemaMetaInfo metaInfo = ctlSchema.getMetaInfo(); ctlSchemaMetaInfoDao.lockRequest(lockOptions).setScope(true).lock(metaInfo); try { ctlSchemaDao.removeById(ctlSchema.getStringId());
.setLockMode( LockMode.PESSIMISTIC_READ ) .setTimeOut( LockOptions.NO_WAIT ) .setScope( true ) .lock( person );
@Override public CtlSchemaMetaInfoDto updateCtlSchemaMetaInfoScope(CtlSchemaMetaInfoDto ctlSchemaMetaInfo) { validateObject(ctlSchemaMetaInfo, "Incorrect ctl schema meta info object"); LOG.debug("Update ctl schema meta info scope with id [{}]", ctlSchemaMetaInfo.getId()); CtlSchemaMetaInfo schemaMetaInfo = ctlSchemaMetaInfoDao.findById(ctlSchemaMetaInfo.getId()); if (schemaMetaInfo != null) { synchronized (this) { ctlSchemaMetaInfoDao.lockRequest(lockOptions).setScope(true).lock(schemaMetaInfo); if (checkScopeUpdate(ctlSchemaMetaInfo, schemaMetaInfo.toDto())) { List<CtlSchemaMetaInfo> others = ctlSchemaMetaInfoDao.findOthersByFqnAndTenantId( ctlSchemaMetaInfo.getFqn(), ctlSchemaMetaInfo.getTenantId(), ctlSchemaMetaInfo .getId()); if (others != null && !others.isEmpty()) { throw new DatabaseProcessingException("Can't update scope of the common type due to " + "an FQN conflict."); } schemaMetaInfo = ctlSchemaMetaInfoDao.updateScope( new CtlSchemaMetaInfo(ctlSchemaMetaInfo)); } return DaoUtil.getDto(schemaMetaInfo); } } else { throw new DatabaseProcessingException("Can't find common type by id."); } }
@Test public void testCascadeLockOnDetachedEntityWithScope() { LOGGER.info("Test lock cascade for detached entity with scope"); //Load the Post entity, which will become detached Post post = doInJPA(entityManager -> { return entityManager.createQuery( "select p " + "from Post p " + "join fetch p.details " + "join fetch p.comments " + "where p.id = :id", Post.class) .setParameter("id", 1L) .getSingleResult(); }); doInJPA(entityManager -> { LOGGER.info("Reattach and lock"); entityManager.unwrap(Session.class) .buildLockRequest( new LockOptions(LockMode.PESSIMISTIC_WRITE)) .setScope(true) .lock(post); //The Post entity graph is attached containsPost(entityManager, post, true); }); doInJPA(entityManager -> { //The detached Post entity changes have been lost Post _post = (Post) entityManager.find(Post.class, 1L); assertEquals("Hibernate Master Class", _post.getTitle()); }); }
@Test public void testCascadeLockOnDetachedEntityWithScope() { LOGGER.info("Test lock cascade for detached entity with scope"); //Load the Post entity, which will become detached Post post = doInJPA(entityManager -> (Post) entityManager.createQuery( "select p " + "from Post p " + "join fetch p.details " + "join fetch p.comments " + "where p.id = :id", Post.class) .setParameter("id", 1L) .getSingleResult()); doInJPA(entityManager -> { LOGGER.info("Reattach and lock"); entityManager.unwrap(Session.class) .buildLockRequest( new LockOptions(LockMode.PESSIMISTIC_WRITE)) .setScope(true) .lock(post); //The Post entity graph is attached containsPost(entityManager, post, true); }); doInJPA(entityManager -> { //The detached Post entity changes have been lost Post _post = (Post) entityManager.find(Post.class, 1L); assertEquals("Hibernate Master Class", _post.getTitle()); }); }
@Test public void testCascadeLockOnDetachedEntityWithScope() { LOGGER.info("Test lock cascade for detached entity with scope"); //Load the Post entity, which will become detached Post post = doInJPA(entityManager -> (Post) entityManager.createQuery( "select p " + "from Post p " + "join fetch p.details " + "join fetch p.comments " + "where p.id = :id", Post.class) .setParameter("id", 1L) .getSingleResult()); doInJPA(entityManager -> { LOGGER.info("Reattach and lock"); entityManager.unwrap(Session.class) .buildLockRequest( new LockOptions(LockMode.PESSIMISTIC_WRITE)) .setScope(true) .lock(post); //The Post entity graph is attached containsPost(entityManager, post, true); }); doInJPA(entityManager -> { //The detached Post entity changes have been lost Post _post = (Post) entityManager.find(Post.class, 1L); assertEquals("Hibernate Master Class", _post.getTitle()); }); }
@Test public void testCascadeLockOnManagedEntityWithAssociationsInitialzied() throws InterruptedException { LOGGER.info("Test lock cascade for managed entity"); doInJPA(entityManager -> { Session session = entityManager.unwrap(Session.class); Post post = (Post) entityManager.createQuery( "select p " + "from Post p " + "join fetch p.details " + "join fetch p.comments " + "where " + " p.id = :id" ).setParameter("id", 1L) .getSingleResult(); session.buildLockRequest(new LockOptions(LockMode.PESSIMISTIC_WRITE)).setScope(true).lock(post); }); }
@Test public void testCascadeLockOnDetachedEntityUninitializedWithScope() { LOGGER.info("Test lock cascade for detached entity with scope"); //Load the Post entity, which will become detached Post post = doInJPA(entityManager -> (Post) entityManager.find(Post.class, 1L)); doInJPA(entityManager -> { LOGGER.info("Reattach and lock entity with associations not initialized"); entityManager.unwrap(Session.class) .buildLockRequest( new LockOptions(LockMode.PESSIMISTIC_WRITE)) .setScope(true) .lock(post); LOGGER.info("Check entities are reattached"); //The Post entity graph is attached containsPost(entityManager, post, true); }); }
@Test public void testCascadeLockOnManagedEntityWithAssociationsInitialzied() throws InterruptedException { LOGGER.info("Test lock cascade for managed entity"); doInJPA(entityManager -> { Session session = entityManager.unwrap(Session.class); Post post = (Post) entityManager.createQuery( "select p " + "from Post p " + "join fetch p.details " + "join fetch p.comments " + "where " + " p.id = :id" ).setParameter("id", 1L) .getSingleResult(); session.buildLockRequest(new LockOptions(LockMode.PESSIMISTIC_WRITE)).setScope(true).lock(post); }); }
@Test public void testCascadeLockOnDetachedEntityUninitializedWithScope() { LOGGER.info("Test lock cascade for detached entity with scope"); //Load the Post entity, which will become detached Post post = doInJPA(entityManager -> (Post) entityManager.find(Post.class, 1L)); doInJPA(entityManager -> { LOGGER.info("Reattach and lock entity with associations not initialized"); entityManager.unwrap(Session.class) .buildLockRequest( new LockOptions(LockMode.PESSIMISTIC_WRITE)) .setScope(true) .lock(post); LOGGER.info("Check entities are reattached"); //The Post entity graph is attached containsPost(entityManager, post, true); }); }
@Test public void testCascadeLockOnManagedEntityWithAssociationsInitialzied() throws InterruptedException { LOGGER.info("Test lock cascade for managed entity"); doInJPA(entityManager -> { Session session = entityManager.unwrap(Session.class); Post post = (Post) entityManager.createQuery( "select p " + "from Post p " + "join fetch p.details " + "join fetch p.comments " + "where " + " p.id = :id" ).setParameter("id", 1L) .getSingleResult(); session.buildLockRequest(new LockOptions(LockMode.PESSIMISTIC_WRITE)).setScope(true).lock(post); }); }
@Test public void testCascadeLockOnDetachedEntityUninitializedWithScope() { LOGGER.info("Test lock cascade for detached entity with scope"); //Load the Post entity, which will become detached Post post = doInJPA(entityManager -> (Post) entityManager.find(Post.class, 1L)); doInJPA(entityManager -> { LOGGER.info("Reattach and lock entity with associations not initialized"); entityManager.unwrap(Session.class) .buildLockRequest( new LockOptions(LockMode.PESSIMISTIC_WRITE)) .setScope(true) .lock(post); LOGGER.info("Check entities are reattached"); //The Post entity graph is attached containsPost(entityManager, post, true); }); }
@Test public void testCascadeLockOnDetachedEntityUninitializedWithScope() { LOGGER.info("Test lock cascade for detached entity with scope"); //Load the Post entity, which will become detached PostComment comment = doInJPA(entityManager -> (PostComment) entityManager.find(PostComment.class, 1L)); doInJPA(entityManager -> { LOGGER.info("Reattach and lock entity with associations not initialized"); entityManager.unwrap(Session.class) .buildLockRequest( new LockOptions(LockMode.PESSIMISTIC_WRITE)) .setScope(true) .lock(comment); LOGGER.info("Check entities are reattached"); }); }
@Test public void testCascadeLockOnManagedEntityWithScope() throws InterruptedException { LOGGER.info("Test lock cascade for managed entity"); doInJPA(entityManager -> { Post post = entityManager.find(Post.class, 1L); entityManager.unwrap(Session.class) .buildLockRequest( new LockOptions(LockMode.PESSIMISTIC_WRITE)) .setScope(true) .lock(post); }); }
@Test public void testCascadeLockOnManagedEntityWithScope() throws InterruptedException { LOGGER.info("Test lock cascade for managed entity"); doInJPA(entityManager -> { Post post = entityManager.find(Post.class, 1L); entityManager.unwrap(Session.class) .buildLockRequest( new LockOptions(LockMode.PESSIMISTIC_WRITE)) .setScope(true) .lock(post); }); }
@Test public void testCascadeLockOnManagedEntityWithScope() throws InterruptedException { LOGGER.info("Test lock cascade for managed entity"); doInJPA(entityManager -> { Post post = entityManager.find(Post.class, 1L); entityManager.unwrap(Session.class) .buildLockRequest( new LockOptions(LockMode.PESSIMISTIC_WRITE)) .setScope(true) .lock(post); }); }
CtlSchemaMetaInfo uniqueMetaInfo = ctlSchemaMetaInfoDao.save( new CtlSchemaMetaInfo(metaInfo)); ctlSchemaMetaInfoDao.lockRequest(lockOptions).setScope(true).lock(uniqueMetaInfo); CtlSchema ctlSchema = new CtlSchema(unSavedSchema); ctlSchema.setMetaInfo(uniqueMetaInfo);
@Override public CTLSchemaDto updateCtlSchema(CTLSchemaDto ctlSchema) { validateCtlSchemaObject(ctlSchema); LOG.debug("Update ctl schema with id [{}]", ctlSchema.getId()); CtlSchema schema = ctlSchemaDao.findById(ctlSchema.getId()); if (schema != null) { synchronized (this) { if (ctlSchema.getVersion() != schema.getVersion()) { throw new DatabaseProcessingException("Can't change version of existing common type " + "version."); } CtlSchemaMetaInfo metaInfo = schema.getMetaInfo(); if (!ctlSchema.getMetaInfo().equals(metaInfo.toDto())) { throw new DatabaseProcessingException("Can't update scope of existing common type " + "version within update procedure."); } ctlSchemaMetaInfoDao.lockRequest(lockOptions).setScope(true).lock(metaInfo); schema.update(ctlSchema); return DaoUtil.getDto(ctlSchemaDao.save(schema, true)); } } else { throw new DatabaseProcessingException("Can't find common type version by id."); } }