本文整理汇总了Java中javax.persistence.LockModeType类的典型用法代码示例。如果您正苦于以下问题:Java LockModeType类的具体用法?Java LockModeType怎么用?Java LockModeType使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
LockModeType类属于javax.persistence包,在下文中一共展示了LockModeType类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: release
import javax.persistence.LockModeType; //导入依赖的package包/类
@Override
public void release() {
final Lock lock = entityManager.find(Lock.class, applicationId, LockModeType.PESSIMISTIC_WRITE);
if (lock == null) {
return;
}
// Only the current owner can release the lock
final String owner = lock.getUniqueId();
if (uniqueId.equals(owner)) {
lock.setUniqueId(null);
lock.setExpirationDate(null);
logger.debug("Releasing {} lock held by {}.", applicationId, uniqueId);
entityManager.persist(lock);
} else {
throw new IllegalStateException("Cannot release lock owned by " + owner);
}
}
示例2: release
import javax.persistence.LockModeType; //导入依赖的package包/类
/**
* {@inheritDoc}
**/
@Override
@Transactional(readOnly = false)
public void release() {
final Lock lock = entityManager.find(Lock.class, applicationId, LockModeType.PESSIMISTIC_WRITE);
if (lock == null) {
return;
}
// Only the current owner can release the lock
final String owner = lock.getUniqueId();
if (uniqueId.equals(owner)) {
lock.setUniqueId(null);
lock.setExpirationDate(null);
logger.debug("Releasing {} lock held by {}.", applicationId, uniqueId);
entityManager.persist(lock);
} else {
throw new IllegalStateException("Cannot release lock owned by " + owner);
}
}
示例3: deleteTicketAndChildren
import javax.persistence.LockModeType; //导入依赖的package包/类
/**
* Delete the TGt and all of its service tickets.
*
* @param ticket the ticket
*/
private void deleteTicketAndChildren(final Ticket ticket) {
final List<TicketGrantingTicketImpl> ticketGrantingTicketImpls = entityManager
.createQuery("select t from TicketGrantingTicketImpl t where t.ticketGrantingTicket.id = :id",
TicketGrantingTicketImpl.class)
.setLockMode(LockModeType.PESSIMISTIC_WRITE)
.setParameter("id", ticket.getId())
.getResultList();
final List<ServiceTicketImpl> serviceTicketImpls = entityManager
.createQuery("select s from ServiceTicketImpl s where s.ticketGrantingTicket.id = :id",
ServiceTicketImpl.class)
.setParameter("id", ticket.getId())
.getResultList();
for (final ServiceTicketImpl s : serviceTicketImpls) {
removeTicket(s);
}
for (final TicketGrantingTicketImpl t : ticketGrantingTicketImpls) {
deleteTicketAndChildren(t);
}
removeTicket(ticket);
}
示例4: aggregate
import javax.persistence.LockModeType; //导入依赖的package包/类
private <S> S aggregate(CriteriaBuilder builder, CriteriaQuery<S> query, Root<E> root, Specification<E> spec, List<Selection<?>> selectionList, LockModeType lockMode) {
if (selectionList != null) {
Predicate predicate = spec.toPredicate(root, query, builder);
if (predicate != null) {
query.where(predicate);
}
query.multiselect(selectionList);
return (S) em.createQuery(query).setLockMode(lockMode).getSingleResult();
}
return null;
}
示例5: findOneByProperty
import javax.persistence.LockModeType; //导入依赖的package包/类
/**
* 根据某些属性获取对象L
* @param name 属性名称
* @param value 属性值
* @param lockMode 对象锁类型
* @return
*/
public T findOneByProperty(String name, Object value, LockModeType lockMode) {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<T> query = cb.createQuery(entityClass);
Root<T> root = query.from(entityClass);
query.where(cb.equal(QueryFormHelper.getPath(root, name), value));
TypedQuery<T> typedQuery = em.createQuery(query);
typedQuery.setLockMode(lockMode);
try {
List<T> list = typedQuery.getResultList();
if (list.isEmpty()) {
return null;
} else {
return list.get(0);
}
} catch (NoResultException e) {
return null;
}
}
示例6: deleteTicketAndChildren
import javax.persistence.LockModeType; //导入依赖的package包/类
private void deleteTicketAndChildren(final Ticket ticket) {
final List<TicketGrantingTicketImpl> ticketGrantingTicketImpls = entityManager
.createQuery("select t from TicketGrantingTicketImpl t where t.ticketGrantingTicket.id = :id",
TicketGrantingTicketImpl.class)
.setLockMode(LockModeType.PESSIMISTIC_WRITE)
.setParameter("id", ticket.getId())
.getResultList();
final List<ServiceTicketImpl> serviceTicketImpls = entityManager
.createQuery("select s from ServiceTicketImpl s where s.ticketGrantingTicket.id = :id",
ServiceTicketImpl.class)
.setParameter("id", ticket.getId())
.getResultList();
for (final ServiceTicketImpl s : serviceTicketImpls) {
removeTicket(s);
}
for (final TicketGrantingTicketImpl t : ticketGrantingTicketImpls) {
deleteTicketAndChildren(t);
}
removeTicket(ticket);
}
示例7: executeTransaction
import javax.persistence.LockModeType; //导入依赖的package包/类
@Override
public void executeTransaction(EntityManager em) throws Exception {
log.debug("Start excecuting RegisterMgrPolicyNotificationTask Task. MC: '" + this.mc.getName() + "'");
this.mc = em.find(ApplianceManagerConnector.class, this.mc.getId(),
LockModeType.PESSIMISTIC_WRITE);
ManagerCallbackNotificationApi mgrApi = null;
try {
mgrApi = this.apiFactoryService.createManagerUrlNotificationApi(this.mc);
mgrApi.createPolicyGroupNotificationRegistration(Server.getApiPort(), RestConstants.OSC_DEFAULT_LOGIN,
this.passwordUtil.getOscDefaultPass());
this.mc.setLastKnownNotificationIpAddress(ServerUtil.getServerIP());
OSCEntityManager.update(em, this.mc, this.txBroadcastUtil);
} finally {
if (mgrApi != null) {
mgrApi.close();
}
}
}
示例8: executeTransaction
import javax.persistence.LockModeType; //导入依赖的package包/类
@Override
public void executeTransaction(EntityManager em) throws Exception {
log.debug("Start excecuting RegisterMgrDomainNotificationTask Task. MC: '" + this.mc.getName() + "'");
this.mc = em.find(ApplianceManagerConnector.class, this.mc.getId(),
LockModeType.PESSIMISTIC_WRITE);
ManagerCallbackNotificationApi mgrApi = null;
try {
mgrApi = this.apiFactoryService.createManagerUrlNotificationApi(this.mc);
mgrApi.createDomainNotificationRegistration(Server.getApiPort(), RestConstants.OSC_DEFAULT_LOGIN,
this.passwordUtil.getOscDefaultPass());
this.mc.setLastKnownNotificationIpAddress(ServerUtil.getServerIP());
OSCEntityManager.update(em, this.mc, this.txBroadcastUtil);
} finally {
if (mgrApi != null) {
mgrApi.close();
}
}
}
示例9: executeTransaction
import javax.persistence.LockModeType; //导入依赖的package包/类
@Override
public void executeTransaction(EntityManager em) throws Exception {
log.debug("Start excecuting RegisterMgrPolicyNotificationTask Task. MC: '" + this.mc.getName() + "'");
this.mc = em.find(ApplianceManagerConnector.class, this.mc.getId(),
LockModeType.PESSIMISTIC_WRITE);
ManagerCallbackNotificationApi mgrApi = null;
try {
mgrApi = this.apiFactoryService.createManagerUrlNotificationApi(this.mc);
mgrApi.updatePolicyGroupNotificationRegistration(this.oldBrokerIp, Server.getApiPort(),
RestConstants.OSC_DEFAULT_LOGIN, this.passwordUtil.getOscDefaultPass());
this.mc.setLastKnownNotificationIpAddress(ServerUtil.getServerIP());
OSCEntityManager.update(em, this.mc, this.txBroadcastUtil);
} finally {
if (mgrApi != null) {
mgrApi.close();
}
}
}
示例10: generateUniqueTag
import javax.persistence.LockModeType; //导入依赖的package包/类
/**
* List all tags used within a VS and locate the next 'minimum' available tag starting with 2. If there are tags
* 'holes' (i.e. for "1,2,3,6,7,9" - 4,5,8,10... will be available for allocation). If no 'holes' available,
* will allocate the next minimum number (10 in our example).
*
* @param session
* database session
* @param vs
* Virtual System Object to get tag for
* @return Minimum and unique tag for given VS.
*/
@SuppressWarnings("unchecked")
public static synchronized Long generateUniqueTag(EntityManager em, VirtualSystem vs) {
vs = em.find(VirtualSystem.class, vs.getId(),
LockModeType.PESSIMISTIC_WRITE);
String sql = "SELECT CONVERT(SUBSTR(tag,LOCATE('-',tag)+1), LONG) AS tag_val "
+ "FROM security_group_interface WHERE virtual_system_fk = " + vs.getId() + " ORDER BY tag_val";
List<Object> list = em.createNativeQuery(sql).getResultList();
// Start with 2 as 1 is reserved in some cases
// TODO: arvindn - Some security partners require tag's larger than 300. Remove once problem is fixed on the
// partners side.
Long prevVal = 301L;
for (Object tag : list) {
long tagValue = ((BigInteger) tag).longValue();
if (tagValue != prevVal) {
return prevVal;
}
prevVal++;
}
return prevVal;
}
示例11: testInitialize
import javax.persistence.LockModeType; //导入依赖的package包/类
@Before
public void testInitialize() throws Exception {
MockitoAnnotations.initMocks(this);
Mockito.when(this.em.getTransaction()).thenReturn(this.tx);
this.txControl.setEntityManager(this.em);
Mockito.when(this.dbMgr.getTransactionalEntityManager()).thenReturn(this.em);
Mockito.when(this.dbMgr.getTransactionControl()).thenReturn(this.txControl);
this.vs = new VirtualSystem();
this.vs.setId(2L);
this.vs.setName("vs");
Appliance appliance = new Appliance();
this.applianceSoftwareVersion = new ApplianceSoftwareVersion(appliance);
this.applianceSoftwareVersion.setApplianceSoftwareVersion("applianceSoftwareVersion");
this.vs.setApplianceSoftwareVersion(this.applianceSoftwareVersion);
Mockito.when(this.em.find(Mockito.eq(VirtualSystem.class), Mockito.eq(this.vs.getId()),
Mockito.eq(LockModeType.PESSIMISTIC_WRITE))).thenReturn(this.vs);
}
示例12: convertToLockModeType
import javax.persistence.LockModeType; //导入依赖的package包/类
/**
* Convert from the Hibernate specific LockMode to the JPA defined LockModeType.
*
* @param lockMode The Hibernate LockMode.
*
* @return The JPA LockModeType
*/
public static LockModeType convertToLockModeType(LockMode lockMode) {
if ( lockMode == LockMode.NONE ) {
return LockModeType.NONE;
}
else if ( lockMode == LockMode.OPTIMISTIC || lockMode == LockMode.READ ) {
return LockModeType.OPTIMISTIC;
}
else if ( lockMode == LockMode.OPTIMISTIC_FORCE_INCREMENT || lockMode == LockMode.WRITE ) {
return LockModeType.OPTIMISTIC_FORCE_INCREMENT;
}
else if ( lockMode == LockMode.PESSIMISTIC_READ ) {
return LockModeType.PESSIMISTIC_READ;
}
else if ( lockMode == LockMode.PESSIMISTIC_WRITE
|| lockMode == LockMode.UPGRADE
|| lockMode == LockMode.UPGRADE_NOWAIT
|| lockMode == LockMode.UPGRADE_SKIPLOCKED) {
return LockModeType.PESSIMISTIC_WRITE;
}
else if ( lockMode == LockMode.PESSIMISTIC_FORCE_INCREMENT
|| lockMode == LockMode.FORCE ) {
return LockModeType.PESSIMISTIC_FORCE_INCREMENT;
}
throw new AssertionFailure( "unhandled lock mode " + lockMode );
}
示例13: acquire
import javax.persistence.LockModeType; //导入依赖的package包/类
/**
* {@inheritDoc}
**/
@Override
@Transactional(readOnly = false)
public boolean acquire() {
Lock lock;
try {
lock = entityManager.find(Lock.class, applicationId, LockModeType.PESSIMISTIC_WRITE);
} catch (final PersistenceException e) {
logger.debug("{} failed querying for {} lock.", uniqueId, applicationId, e);
return false;
}
boolean result = false;
if (lock != null) {
final DateTime expDate = new DateTime(lock.getExpirationDate());
if (lock.getUniqueId() == null) {
// No one currently possesses lock
logger.debug("{} trying to acquire {} lock.", uniqueId, applicationId);
result = acquire(entityManager, lock);
} else if (new DateTime().isAfter(expDate)) {
// Acquire expired lock regardless of who formerly owned it
logger.debug("{} trying to acquire expired {} lock.", uniqueId, applicationId);
result = acquire(entityManager, lock);
}
} else {
// First acquisition attempt for this applicationId
logger.debug("Creating {} lock initially held by {}.", applicationId, uniqueId);
result = acquire(entityManager, new Lock());
}
return result;
}
示例14: getRawTicket
import javax.persistence.LockModeType; //导入依赖的package包/类
/**
* Gets the ticket from the database, as is.
*
* @param ticketId the ticket id
* @return the raw ticket
*/
private Ticket getRawTicket(final String ticketId) {
try {
if (ticketId.startsWith(this.ticketGrantingTicketPrefix)) {
return entityManager.find(TicketGrantingTicketImpl.class, ticketId, LockModeType.PESSIMISTIC_WRITE);
}
return entityManager.find(ServiceTicketImpl.class, ticketId);
} catch (final Exception e) {
logger.error("Error getting ticket {} from registry.", ticketId, e);
}
return null;
}
示例15: acquire
import javax.persistence.LockModeType; //导入依赖的package包/类
@Override
public boolean acquire() {
final Lock lock;
try {
lock = this.entityManager.find(Lock.class, this.applicationId, LockModeType.OPTIMISTIC);
} catch (final Exception e) {
LOGGER.debug("[{}] failed querying for [{}] lock.", this.uniqueId, this.applicationId, e);
return false;
}
boolean result = false;
if (lock != null) {
final ZonedDateTime expDate = lock.getExpirationDate();
if (lock.getUniqueId() == null) {
// No one currently possesses lock
LOGGER.debug("[{}] trying to acquire [{}] lock.", this.uniqueId, this.applicationId);
result = acquire(lock);
} else if (expDate == null || ZonedDateTime.now(ZoneOffset.UTC).isAfter(expDate)) {
// Acquire expired lock regardless of who formerly owned it
LOGGER.debug("[{}] trying to acquire expired [{}] lock.", this.uniqueId, this.applicationId);
result = acquire(lock);
}
} else {
// First acquisition attempt for this applicationId
LOGGER.debug("Creating [{}] lock initially held by [{}].", applicationId, uniqueId);
result = acquire(new Lock());
}
return result;
}