Example usage for javax.persistence EntityTransaction begin

List of usage examples for javax.persistence EntityTransaction begin

Introduction

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

Prototype

public void begin();

Source Link

Document

Start a resource transaction.

Usage

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

public static void main(String[] args) {

    try {// ww w .  j a  v  a  2  s  . co 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  ww .  j av  a2s  . com*/
        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 {/*from w w w . j a  v  a  2s .  c om*/
        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 {//from  w  w  w  . ja  v  a  2 s .c om
        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:org.isatools.isatab.commandline.ContentsShellCommand.java

@SuppressWarnings("static-access")
public static void main(String[] args) {

    try {//ww w  .  j av  a2s  . c  o  m
        Options clopts = createCommonOptions();
        clopts.addOption(OptionBuilder.withDescription("List all the studies and their permissions/ownership")
                .withLongOpt("studies").create("s"));
        clopts.addOption(OptionBuilder.withDescription("List all the users").withLongOpt("users").create("u"));

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

        boolean isStudies = cmdl.hasOption("s"), isUsers = cmdl.hasOption("u");
        if (!isStudies && !isUsers) {
            printUsage(clopts);
            System.exit(1);
        }

        args = cmdl.getArgs();
        setup(args);
        setupLog4JPath(cmdl, null);

        // Need to initialize this here, otherwise above config will fail
        log = Logger.getLogger(ContentsShellCommand.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);
        if (isStudies) {
            List<Study> studies = permMgr.getStudies();
            if (studies == null || studies.size() == 0) {
                log.info("No study in the BII DB");
            } else {
                for (Study s : studies) {
                    log.info(PermissionManager.formatStudy(s));
                }
            }
        } else {
            List<Person> persons = permMgr.getUsers();
            if (persons == null || persons.size() == 0) {
                log.info("No user in the BII DB");
            } else {
                for (Person u : persons) {
                    log.info(PermissionManager.formatUser(u));
                }
            }
        }

        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:es.us.isa.ideas.utilities.PopulateDatabase.java

public static void main(String[] args) {

    ApplicationContext ctx;//from w w  w  .j  a  v a 2  s  .c  o  m
    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;/*  w ww  . j a v a  2s  .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  w w  w.  j a  v  a  2 s .  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.isatools.isatab.commandline.PersistenceShellCommand.java

/**
 * TODO: this is a patch used until we are able to make the auto-indexing upon load feature working
 * It is called after persistence./*w ww  . j  a va 2 s. c  o  m*/
 *
 * @param store               must contain the studies that have to be reindexed (with proper accession).
 * @param hibernateProperties - The hibernate properties indicating DB properties and Index location/Strategy,
 *                            and so forth.
 */
public static void reindexStudies(BIIObjectStore store, Properties hibernateProperties) {
    // Need to initialize this here, otherwise above config will fail
    log = Logger.getLogger(PersistenceShellCommand.class);

    hibernateProperties.setProperty("hibernate.search.indexing_strategy", "event");
    hibernateProperties.setProperty("hibernate.hbm2ddl.auto", "update");
    hibernateProperties.setProperty("hbm2ddl.drop", "false");

    EntityManagerFactory emf1 = Persistence.createEntityManagerFactory("BIIEntityManager", hibernateProperties);
    EntityManager entityManager1 = emf1.createEntityManager();

    StudyDAO studyDAO = DaoFactory.getInstance(entityManager1).getStudyDAO();
    FullTextEntityManager fullTxtEm = Search.getFullTextEntityManager(entityManager1);
    EntityTransaction tnx = entityManager1.getTransaction();

    tnx.begin();
    for (Study study : store.valuesOfType(Study.class)) {
        Study dbStudy = studyDAO.getByAcc(study.getAcc());
        if (dbStudy != null) {
            out.println("Indexing Study #" + dbStudy.getAcc());
            fullTxtEm.index(dbStudy);
            log.info("Indexing of Study # " + dbStudy.getAcc() + " is complete!");
        }

    }
    log.info("Commiting & closing Entity Manager.");
    tnx.commit();
    entityManager1.close();
}

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();
    try {//  www . ja v  a2  s .  co m
        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);
    }
}