Java Code Examples for org.springframework.orm.jpa.JpaSystemException#getRootCause()

The following examples show how to use org.springframework.orm.jpa.JpaSystemException#getRootCause() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VmNicFactory.java    From zstack with Apache License 2.0 6 votes vote down vote up
private VmNicVO persistAndRetryIfMacCollision(VmNicVO vo) {
    int tries = 5;
    while (tries-- > 0) {
        try {
            return dbf.persistAndRefresh(vo);
        } catch (JpaSystemException e) {
            if (e.getRootCause() instanceof MySQLIntegrityConstraintViolationException &&
                    e.getRootCause().getMessage().contains("Duplicate entry")) {
                logger.debug(String.format("Concurrent mac allocation. Mac[%s] has been allocated, try allocating another one. " +
                        "The error[Duplicate entry] printed by jdbc.spi.SqlExceptionHelper is no harm, " +
                        "we will try finding another mac", vo.getMac()));
                logger.trace("", e);
                vo.setMac(NetworkUtils.generateMacWithDeviceId((short) vo.getDeviceId()));
            } else {
                throw e;
            }
        }
    }
    return null;
}
 
Example 2
Source File: LdapManagerImpl.java    From zstack with Apache License 2.0 5 votes vote down vote up
private void handle(APICreateLdapBindingMsg msg) {
    APICreateLdapBindingEvent evt = new APICreateLdapBindingEvent(msg.getId());

    // account check
    SimpleQuery<AccountVO> sq = dbf.createQuery(AccountVO.class);
    sq.add(AccountVO_.uuid, SimpleQuery.Op.EQ, msg.getAccountUuid());
    AccountVO avo = sq.find();
    if (avo == null) {
        evt.setError(err(LdapErrors.CANNOT_FIND_ACCOUNT,
                String.format("cannot find the specified account[uuid:%s]", msg.getAccountUuid())));
        bus.publish(evt);
        return;
    }

    String ldapUseAsLoginName = ldapUtil.getLdapUseAsLoginName();

    // bind op
    LdapTemplateContextSource ldapTemplateContextSource = ldapUtil.readLdapServerConfiguration();
    String fullDn = msg.getLdapUid();
    if (!ldapUtil.validateDnExist(ldapTemplateContextSource, fullDn)) {
        throw new OperationFailureException(err(LdapErrors.UNABLE_TO_GET_SPECIFIED_LDAP_UID,
                "cannot find dn[%s] on LDAP/AD server[Address:%s, BaseDN:%s].", fullDn,
                String.join(", ", ldapTemplateContextSource.getLdapContextSource().getUrls()),
                ldapTemplateContextSource.getLdapContextSource().getBaseLdapPathAsString()));
    }
    try {
        evt.setInventory(bindLdapAccount(msg.getAccountUuid(), fullDn));
        logger.info(String.format("create ldap binding[ldapUid=%s, ldapUseAsLoginName=%s] success", fullDn, ldapUseAsLoginName));
    } catch (JpaSystemException e) {
        if (e.getRootCause() instanceof MySQLIntegrityConstraintViolationException) {
            evt.setError(err(LdapErrors.BIND_SAME_LDAP_UID_TO_MULTI_ACCOUNT,
                    "The ldap uid has been bound to an account. "));
        } else {
            throw e;
        }
    }
    bus.publish(evt);
}
 
Example 3
Source File: L3NetworkManagerImpl.java    From zstack with Apache License 2.0 5 votes vote down vote up
private UsedIpInventory reserveIpv6(IpRangeVO ipRange, String ip) {
    try {
        UsedIpVO vo = new UsedIpVO();
        //vo.setIpInLong(NetworkUtils.ipv4StringToLong(ip));
        String uuid = ipRange.getUuid() + ip;
        uuid = UUID.nameUUIDFromBytes(uuid.getBytes()).toString().replaceAll("-", "");
        vo.setUuid(uuid);
        vo.setIpRangeUuid(ipRange.getUuid());
        vo.setIp(IPv6NetworkUtils.getIpv6AddressCanonicalString(ip));
        vo.setL3NetworkUuid(ipRange.getL3NetworkUuid());
        vo.setNetmask(ipRange.getNetmask());
        vo.setGateway(ipRange.getGateway());
        vo.setIpVersion(IPv6Constants.IPv6);
        vo = dbf.persistAndRefresh(vo);
        return UsedIpInventory.valueOf(vo);
    } catch (JpaSystemException e) {
        if (e.getRootCause() instanceof MySQLIntegrityConstraintViolationException) {
            logger.debug(String.format("Concurrent ip allocation. " +
                    "Ip[%s] in ip range[uuid:%s] has been allocated, try allocating another one. " +
                    "The error[Duplicate entry] printed by jdbc.spi.SqlExceptionHelper is no harm, " +
                    "we will try finding another ip", ip, ipRange.getUuid()));
            logger.trace("", e);
        } else {
            throw e;
        }
        return null;
    }
}
 
Example 4
Source File: L3NetworkManagerImpl.java    From zstack with Apache License 2.0 5 votes vote down vote up
private UsedIpInventory reserveIpv4(IpRangeVO ipRange, String ip, boolean allowDuplicatedAddress) {
    try {
        UsedIpVO vo = new UsedIpVO(ipRange.getUuid(), ip);
        vo.setIpInLong(NetworkUtils.ipv4StringToLong(ip));
        String uuid;
        if (allowDuplicatedAddress) {
            uuid = Platform.getUuid();
        } else {
            uuid = ipRange.getUuid() + ip;
            uuid = UUID.nameUUIDFromBytes(uuid.getBytes()).toString().replaceAll("-", "");
        }
        vo.setUuid(uuid);
        vo.setL3NetworkUuid(ipRange.getL3NetworkUuid());
        vo.setNetmask(ipRange.getNetmask());
        vo.setGateway(ipRange.getGateway());
        vo.setIpVersion(IPv6Constants.IPv4);
        vo = dbf.persistAndRefresh(vo);
        return UsedIpInventory.valueOf(vo);
    } catch (JpaSystemException e) {
        if (e.getRootCause() instanceof MySQLIntegrityConstraintViolationException) {
            logger.debug(String.format("Concurrent ip allocation. " +
                    "Ip[%s] in ip range[uuid:%s] has been allocated, try allocating another one. " +
                    "The error[Duplicate entry] printed by jdbc.spi.SqlExceptionHelper is no harm, " +
                    "we will try finding another ip", ip, ipRange.getUuid()));
            logger.trace("", e);
        } else {
            throw e;
        }
        return null;
    }
}