Example usage for javax.persistence EntityTransaction commit

List of usage examples for javax.persistence EntityTransaction commit

Introduction

In this page you can find the example usage for javax.persistence EntityTransaction commit.

Prototype

public void commit();

Source Link

Document

Commit the current resource transaction, writing any unflushed changes to the database.

Usage

From source file:com.eucalyptus.images.Images.java

/***
 * @param imageId: id of an image already registered as pending state
 * @param userFullName/*from w  w w. ja  v a  2s  .  c o m*/
 * @param blockDeviceMappings: the mapping that contains the root device
 * @return
 * @throws EucalyptusCloudException
 */
public static ImageInfo updateWithDeviceMapping(String imageId, UserFullName userFullName,
        final String rootDeviceName, final List<BlockDeviceMappingItemType> blockDeviceMappings)
        throws EucalyptusCloudException {
    // Block device mappings have been verified before control gets here. 
    // If anything has changed with regard to the snapshot state, it will be caught while data structures for the image.
    final BlockDeviceMappingItemType rootBlockDevice = Iterables.find(blockDeviceMappings,
            findEbsRoot(rootDeviceName));
    final String snapshotId = ResourceIdentifiers.tryNormalize()
            .apply(rootBlockDevice.getEbs().getSnapshotId());
    try {
        Snapshot snap = Transactions.find(Snapshot.named(userFullName, snapshotId));
        if (!userFullName.getUserId().equals(snap.getOwnerUserId())) {
            throw new EucalyptusCloudException("Failed to create image from specified block device mapping: "
                    + rootBlockDevice + " because of: you must be the owner of the source snapshot.");
        }

        Integer suppliedVolumeSize = rootBlockDevice.getEbs().getVolumeSize() != null
                ? rootBlockDevice.getEbs().getVolumeSize()
                : snap.getVolumeSize();

        Long imageSizeBytes = suppliedVolumeSize * 1024l * 1024l * 1024l;
        Boolean targetDeleteOnTermination = Boolean.TRUE
                .equals(rootBlockDevice.getEbs().getDeleteOnTermination());

        BlockStorageImageInfo ret = null;
        final EntityTransaction tx = Entities.get(BlockStorageImageInfo.class);
        try {
            ret = (BlockStorageImageInfo) Entities.uniqueResult(BlockStorageImageInfo.named(imageId));
            final List<DeviceMapping> mappings = Lists.transform(blockDeviceMappings,
                    Images.deviceMappingGenerator(ret, suppliedVolumeSize));
            ret.getDeviceMappings().addAll(mappings);
            ret.setSnapshotId(snap.getDisplayName());
            ret.setDeleteOnTerminate(targetDeleteOnTermination);
            ret.setImageSizeBytes(imageSizeBytes);
            ret.setRootDeviceName(rootDeviceName);
            ret.setState(ImageMetadata.State.available);
            Entities.persist(ret);
            tx.commit();
            LOG.info("Registering image pk=" + ret.getDisplayName() + " ownerId=" + userFullName);
        } catch (Exception e) {
            tx.rollback();
            throw new EucalyptusCloudException(
                    "Failed to register image using snapshot: " + snapshotId + " because of: " + e.getMessage(),
                    e);
        }
        return ret;
    } catch (TransactionExecutionException ex) {
        throw new EucalyptusCloudException("Failed to update image with specified block device mapping: "
                + rootBlockDevice + " because of: " + ex.getMessage());
    } catch (ExecutionException ex) {
        LOG.error(ex, ex);
        throw new EucalyptusCloudException("Failed to update image with specified block device mapping: "
                + rootBlockDevice + " because of: " + ex.getMessage());
    }
}

From source file:portal.api.impl.PortalJpaController.java

public void saveMANOplatform(MANOplatform mp) {
    logger.info("Will save MANOplatform = " + mp.getName());

    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();
    entityTransaction.begin();/*ww w. j a  v  a 2  s  .c  o m*/
    entityManager.persist(mp);
    entityManager.flush();
    entityTransaction.commit();

}

From source file:portal.api.impl.PortalJpaController.java

public void saveProperty(PortalProperty p) {
    logger.info("Will PortalProperty = " + p.getName());

    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();
    entityTransaction.begin();//from w  w w.j  a  va  2  s. co  m

    entityManager.persist(p);
    entityManager.flush();
    entityTransaction.commit();

}

From source file:portal.api.impl.PortalJpaController.java

public ExperimentOnBoardDescriptor updateExperimentOnBoardDescriptor(ExperimentOnBoardDescriptor c) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();/* w w  w.  j ava  2 s  . co  m*/
    ExperimentOnBoardDescriptor resis = entityManager.merge(c);
    entityTransaction.commit();

    return resis;
}

From source file:com.github.jinahya.persistence.ShadowTest.java

@Test(enabled = true, invocationCount = 1)
public void testNassword0() {
    final EntityManager manager = LocalPU.createEntityManager();
    try {/*from   w  w  w .jav  a 2s .  c o  m*/
        final EntityTransaction transaction = manager.getTransaction();
        transaction.begin();
        try {
            final String username = newUsername(manager);
            final byte[] password = newPassword();
            Shadow shadow = persistInstance(manager, username, password);
            Assert.assertTrue(shadow.puthenticate(shadow, password));
            System.out.println("=========================================");
            LOGGER.log(Level.INFO, "mortons: {0}", MORTONS(manager, 0, 1024));
            final byte[] nassword = newPassword();
            shadow.nassword(shadow, password, nassword);
            shadow = manager.merge(shadow);
            manager.flush();
            System.out.println("=========================================");
            LOGGER.log(Level.INFO, "mortons: {0}", MORTONS(manager, 0, 1024));
            Assert.assertFalse(shadow.puthenticate(shadow, password));
            Assert.assertTrue(shadow.puthenticate(shadow, nassword));
            transaction.commit();
        } catch (Exception e) {
            transaction.rollback();
            e.printStackTrace(System.err);
            Assert.fail(e.getMessage());
        }
    } finally {
        manager.close();
    }
}

From source file:portal.api.impl.PortalJpaController.java

public void saveInstalledVxF(InstalledVxF is) {
    logger.info("Will create InstalledVxF = " + is.getUuid());

    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();//w w w . j  a v  a2 s. c o m

    entityManager.persist(is);
    entityManager.flush();
    entityTransaction.commit();
}

From source file:portal.api.impl.PortalJpaController.java

public void saveUser(PortalUser bu) {
    logger.info("Will save PortalUser = " + bu.getName());

    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();

    entityTransaction.begin();/*from   w  w w . j a  v  a  2s  .c o  m*/

    entityManager.persist(bu);

    entityManager.flush();
    entityTransaction.commit();

}

From source file:portal.api.impl.PortalJpaController.java

public void saveMANOprovider(MANOprovider mprovider) {
    logger.info("Will save MANOprovider = " + mprovider.getName());

    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();
    entityTransaction.begin();/*  w ww . j a v a2  s  .c om*/
    entityManager.persist(mprovider);
    entityManager.flush();
    entityTransaction.commit();

}

From source file:portal.api.impl.PortalJpaController.java

public void saveVxFOnBoardedDescriptor(VxFOnBoardedDescriptor mprovider) {
    logger.info("Will save VxFOnBoardedDescriptor = " + mprovider.getDeployId());

    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();
    entityTransaction.begin();//from  www.  ja v a  2s .  co  m
    entityManager.persist(mprovider);
    entityManager.flush();
    entityTransaction.commit();

}

From source file:portal.api.impl.PortalJpaController.java

public void saveExperimentOnBoardDescriptor(ExperimentOnBoardDescriptor mprovider) {
    logger.info("Will save ExperimentOnBoardDescriptors = " + mprovider.getDeployId());

    EntityManager entityManager = entityManagerFactory.createEntityManager();

    EntityTransaction entityTransaction = entityManager.getTransaction();
    entityTransaction.begin();/*from   ww w . ja  v  a2 s .c o  m*/
    entityManager.persist(mprovider);
    entityManager.flush();
    entityTransaction.commit();

}