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:org.isatools.isatab.commandline.UserDelShellCommand.java

public static void main(String[] args) {

    try {//from w  ww  .j  a v a  2s .c o m
        Options clopts = createCommonOptions();

        CommandLine cmdl = AbstractImportLayerShellCommand.parseCommandLine(clopts, args,
                UserDelShellCommand.class);

        args = cmdl.getArgs();
        if (args == null || args.length != 1) {
            printUsage(clopts);
            System.exit(1);
        }

        setup(args);
        setupLog4JPath(cmdl, null);

        // Need to initialize this here, otherwise above config will fail
        log = Logger.getLogger(UserDelShellCommand.class);

        Properties hibProps = AbstractImportLayerShellCommand.getHibernateProperties();
        hibProps.setProperty("hibernate.search.indexing_strategy", "event");
        hibProps.setProperty("hibernate.hbm2ddl.auto", "update");
        hibProps.setProperty("hbm2ddl.drop", "false");

        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("BIIEntityManager",
                hibProps);
        EntityManager entityManager = entityManagerFactory.createEntityManager();

        EntityTransaction transaction = entityManager.getTransaction();
        transaction.begin();

        PermissionManager permMgr = new PermissionManager(entityManager);
        permMgr.deleteUser(args[0]);

        transaction.commit();
        entityManager.close();

        log.info("User deleted.");
        log.info("\n");

        System.exit(0);
    } catch (Exception ex) {
        String msg = "ERROR: problem while running the Permission Manager: " + ex.getMessage();
        if (log == null) {
            out.println(msg + "\n");
            ex.printStackTrace();
        } else {
            log.fatal(msg, ex);
        }
        System.exit(1);
    }
}

From source file:org.isatools.isatab.commandline.PermModShellCommand.java

public static void main(String[] args) {

    try {//from  w w  w .ja  v a  2s. co  m
        Options clopts = createCommonOptions();
        PermissionManager.createPermModOptions(clopts);

        if (args == null || args.length == 0) {
            printUsage(clopts);
            System.exit(1);
        }

        CommandLine cmdl = AbstractImportLayerShellCommand.parseCommandLine(clopts, args,
                PermModShellCommand.class);

        args = cmdl.getArgs();
        setup(args);

        setupLog4JPath(cmdl, null);
        // Need to initialize this here, otherwise above config will fail
        log = Logger.getLogger(PermModShellCommand.class);

        Properties hibProps = AbstractImportLayerShellCommand.getHibernateProperties();
        hibProps.setProperty("hibernate.search.indexing_strategy", "event");
        hibProps.setProperty("hibernate.hbm2ddl.auto", "update");
        hibProps.setProperty("hbm2ddl.drop", "false");

        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("BIIEntityManager",
                hibProps);
        EntityManager entityManager = entityManagerFactory.createEntityManager();

        EntityTransaction transaction = entityManager.getTransaction();
        transaction.begin();

        PermissionManager permMgr = new PermissionManager(entityManager);
        permMgr.setStudyOwners(cmdl);
        permMgr.setVisibility(cmdl);

        transaction.commit();
        entityManager.close();

        log.info("\n");

        System.exit(0);
    } catch (Exception ex) {
        String msg = "ERROR: problem while running the Permission Manager: " + ex.getMessage();
        if (log == null) {
            out.println(msg + "\n");
            ex.printStackTrace();
        } else {
            log.fatal(msg, ex);
        }
        System.exit(1);
    }
}

From source file:org.isatools.isatab.commandline.UserAddShellCommand.java

public static void main(String[] args) {

    try {/*w  w w .j av  a2  s  .  c o m*/
        Options clopts = createCommonOptions();
        PermissionManager.createUserDefOptions(clopts);

        CommandLine cmdl = AbstractImportLayerShellCommand.parseCommandLine(clopts, args,
                UserAddShellCommand.class);

        args = cmdl.getArgs();
        if (args == null || args.length != 1) {
            printUsage(clopts);
            System.exit(1);
        }

        setup(args);
        setupLog4JPath(cmdl, null);

        // Need to initialize this here, otherwise above config will fail
        log = Logger.getLogger(UserAddShellCommand.class);

        Properties hibProps = AbstractImportLayerShellCommand.getHibernateProperties();
        hibProps.setProperty("hibernate.search.indexing_strategy", "event");
        hibProps.setProperty("hibernate.hbm2ddl.auto", "update");
        hibProps.setProperty("hbm2ddl.drop", "false");

        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("BIIEntityManager",
                hibProps);
        EntityManager entityManager = entityManagerFactory.createEntityManager();

        EntityTransaction transaction = entityManager.getTransaction();
        transaction.begin();

        PermissionManager permMgr = new PermissionManager(entityManager);
        Person user = permMgr.createNewUserFromOptions(args[0], cmdl);
        permMgr.addUser(user);

        transaction.commit();
        entityManager.close();

        log.info(PermissionManager.formatUser(user));
        log.info("User added.");
        log.info("\n");

        System.exit(0);
    } catch (Exception ex) {
        String msg = "ERROR: problem while running the Permission Manager: " + ex.getMessage();
        if (log == null) {
            out.println(msg + "\n");
            ex.printStackTrace();
        } else {
            log.fatal(msg, ex);
        }
        System.exit(1);
    }
}

From source file:org.isatools.isatab.commandline.UserModShellCommand.java

public static void main(String[] args) {

    try {//w w w . jav a2 s .c o  m
        Options clopts = createCommonOptions();
        PermissionManager.createUserDefOptions(clopts);

        CommandLine cmdl = AbstractImportLayerShellCommand.parseCommandLine(clopts, args,
                UserModShellCommand.class);

        args = cmdl.getArgs();
        if (args == null || args.length != 1) {
            printUsage(clopts);
            System.exit(1);
        }

        setup(args);
        setupLog4JPath(cmdl, null);

        // Need to initialize this here, otherwise above config will fail
        log = Logger.getLogger(UserModShellCommand.class);

        Properties hibProps = AbstractImportLayerShellCommand.getHibernateProperties();
        hibProps.setProperty("hibernate.search.indexing_strategy", "event");
        hibProps.setProperty("hibernate.hbm2ddl.auto", "update");
        hibProps.setProperty("hbm2ddl.drop", "false");

        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("BIIEntityManager",
                hibProps);
        EntityManager entityManager = entityManagerFactory.createEntityManager();

        EntityTransaction transaction = entityManager.getTransaction();
        transaction.begin();

        PermissionManager permMgr = new PermissionManager(entityManager);
        Person user = permMgr.createNewUserFromOptions(args[0], cmdl);
        permMgr.updateUser(user);

        transaction.commit();

        log.info("User changed, new details:");
        log.info(PermissionManager.formatUser(permMgr.getUserByLogin(user.getUserName())));
        log.info("\n");

        entityManager.close();
        System.exit(0);
    } catch (Exception ex) {
        String msg = "ERROR: problem while running the Permission Manager: " + ex.getMessage();
        if (log == null) {
            out.println(msg + "\n");
            ex.printStackTrace();
        } else {
            log.fatal(msg, ex);
        }
        System.exit(1);
    }
}

From source file:es.us.isa.ideas.utilities.PopulateDatabase.java

public static void main(String[] args) {

    ApplicationContext ctx;//from ww  w .  j  a  va2  s.c om
    EntityManagerFactory emf;
    EntityManager em;
    EntityTransaction et;

    ctx = new ClassPathXmlApplicationContext("utilities/PopulateDatabase.xml");

    emf = Persistence.createEntityManagerFactory("persistenceUnit");
    em = emf.createEntityManager();
    et = em.getTransaction();

    et.begin();
    try {
        for (Entry<String, Object> entry : ctx.getBeansWithAnnotation(Entity.class).entrySet()) {
            em.persist(entry.getValue());
            System.out.println(String.format("Persisting (%s, %s@%d)", entry.getKey(),
                    entry.getValue().getClass().getName(), entry.getValue().hashCode()));
        }
        et.commit();
    } catch (Exception oops) {
        oops.printStackTrace();
        et.rollback();
        oops.printStackTrace();
    } finally {
        if (em.isOpen())
            em.close();
        if (emf.isOpen())
            emf.close();
        ((ClassPathXmlApplicationContext) ctx).close();
    }
}

From source file:utilities.PopulateDatabase.java

public static void main(String[] args) throws Throwable {
    ApplicationContext applicationContext;
    EntityManagerFactory entityManagerFactory;
    EntityManager entityManager;//from w  ww .j av a  2 s .  com
    EntityTransaction entityTransaction;

    applicationContext = new ClassPathXmlApplicationContext("classpath:PopulateDatabase.xml");

    entityManagerFactory = Persistence.createEntityManagerFactory(PersistenceUnit);
    entityManager = entityManagerFactory.createEntityManager();
    entityTransaction = entityManager.getTransaction();

    initialise(entityManagerFactory, entityManager);

    entityTransaction.begin();
    try {
        for (Entry<String, Object> entry : applicationContext.getBeansWithAnnotation(Entity.class).entrySet()) {
            String beanName;
            DomainEntity entity;

            beanName = entry.getKey();
            entity = (DomainEntity) entry.getValue();
            entityManager.persist(entity);
            System.out.println(String.format("Persisting (%s, %s, %d)", beanName, entity.getClass().getName(),
                    entity.getId()));
        }
        entityTransaction.commit();
    } catch (Exception oops) {
        oops.printStackTrace();
        entityTransaction.rollback();
    } finally {
        if (entityManager.isOpen())
            entityManager.close();
        if (entityManagerFactory.isOpen())
            entityManagerFactory.close();
    }
}

From source file:org.isatools.isatab.commandline.PersistenceShellCommand.java

public static void main(String[] args) {
    EntityTransaction transaction = null;
    try {//from  www  .  j  ava 2s  .  c  om

        Options clopts = createCommonOptions();
        CommandLine cmdl = AbstractImportLayerShellCommand.parseCommandLine(clopts, args,
                PersistenceShellCommand.class);

        args = cmdl.getArgs();
        if (args == null || args.length == 0) {
            printUsage(clopts);
            System.exit(1);
        }

        setup(args);
        setupLog4JPath(cmdl, null);

        // Need to initialize this here, otherwise above config will fail
        log = Logger.getLogger(PersistenceShellCommand.class);

        Properties hibProps = AbstractImportLayerShellCommand.getHibernateProperties();
        hibProps.setProperty("hibernate.search.indexing_strategy", "manual");
        hibProps.setProperty("hibernate.hbm2ddl.auto", "update");
        hibProps.setProperty("hbm2ddl.drop", "false");

        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("BIIEntityManager",
                hibProps);
        EntityManager entityManager = entityManagerFactory.createEntityManager();

        BIIObjectStore store = loadIsaTab();
        log.info(i18n.msg("mapping_done_now_persisting", store.size()));

        ISATABPersister persister = new ISATABPersister(store, DaoFactory.getInstance(entityManager));
        transaction = entityManager.getTransaction();
        transaction.begin();
        Timestamp ts = persister.persist(sourceDirPath);
        transaction.commit();
        entityManager.close();

        reindexStudies(store, hibProps);

        log.info("\n\n" + i18n.msg("mapping_done_data_saved_in_db"));
        log.info("\n\n" + i18n.msg("submission_done_ts_reported", "" + ts.getTime(),
                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").format(ts) + "." + ts.getNanos()) + "\n");
        System.exit(0);
    } catch (Exception ex) {
        String msg = "ERROR: problem while running the ISATAB loader: " + ex.getMessage();
        if (log == null) {
            out.println(msg + "\n");
            ex.printStackTrace();
        } else {
            log.fatal(msg, ex);
        }
        System.exit(1);
    }
}

From source file:org.apache.juddi.validation.ValidateValueSetValidation.java

public static Tmodel GetTModel_MODEL_IfExists(String tmodelKey) throws ValueNotAllowedException {
    EntityManager em = PersistenceManager.getEntityManager();

    Tmodel model = null;//from www .  j  a  v  a 2s . com
    if (em == null) {
        //this is normally the Install class firing up
        log.warn(new ErrorMessage("errors.tmodel.ReferentialIntegrityNullEM"));
        return null;
    } else {

        EntityTransaction tx = em.getTransaction();
        try {

            tx.begin();
            model = em.find(org.apache.juddi.model.Tmodel.class, tmodelKey);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }

    }
    return model;
}

From source file:org.apache.oozie.tools.OozieDBImportCLI.java

private static void importFrom(EntityManager entityManager, ZipFile zipFile, String table, Class<?> clazz,
        String fileName) throws JPAExecutorException, IOException {
    EntityTransaction transaction = entityManager.getTransaction();
    transaction.begin();//w ww  .j a v  a2 s  . c o  m
    try {
        int size = importFromJSONtoDB(entityManager, zipFile, fileName, clazz);
        transaction.commit();
        System.out.println(size + " rows imported to " + table);
    } catch (Exception e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        throw new RuntimeException("Import failed to table " + table + ".", e);
    }
}

From source file:com.ecarride.App.java

public static void ResetDriverWorkStatus() {
    Iterator<TlcDriver> iterator = tlcDrivers.iterator();
    while (iterator.hasNext()) {
        TlcDriver temp = iterator.next();
        if (!temp.isInTrouble())
            continue;
        EntityTransaction updateTransaction = entityManager.getTransaction();
        updateTransaction.begin();// w  w  w. j  a v  a2s.  c  o m
        Query query = entityManager
                .createQuery("UPDATE Driver d SET d.baseApproved = 0, d.status = 0" + "WHERE d.id= :id");
        query.setParameter("id", temp.getDriver().getId());
        query.executeUpdate();
        updateTransaction.commit();
    }
}