Example usage for javax.transaction UserTransaction commit

List of usage examples for javax.transaction UserTransaction commit

Introduction

In this page you can find the example usage for javax.transaction UserTransaction commit.

Prototype

void commit() throws RollbackException, HeuristicMixedException, HeuristicRollbackException, SecurityException,
        IllegalStateException, SystemException;

Source Link

Document

Complete the transaction associated with the current thread.

Usage

From source file:org.minig.cache.JDBCDataStore.java

@Override
public int getCacheId(String userId) {
    ResultSet rs = null;//  w  w w.  j a va 2  s. c  o m
    PreparedStatement st = null;
    PreparedStatement st2 = null;
    Connection con = null;
    int cacheId = 0;

    UserTransaction ut = getUserTransaction();
    try {
        ut.begin();
        con = getConnection();
        st = con.prepareStatement("SELECT id FROM minig_cache WHERE user_id = ?");
        st.setString(1, userId);
        rs = st.executeQuery();
        if (rs.next()) {
            cacheId = rs.getInt(1);
        }

        if (cacheId == 0) {
            st2 = con.prepareStatement("INSERT INTO minig_cache (user_id) VALUES (?)");

            st2.setString(1, userId);
            st2.executeUpdate();
            cacheId = CacheActivator.get().generatedKey(con);
        }
        ut.commit();
    } catch (Exception e) {
        logger.error("Error executing query creating cacheId", e);
        JDBCUtils.rollback(ut);
    } finally {
        JDBCUtils.cleanup(null, st2, null);
        JDBCUtils.cleanup(con, st, rs);
    }
    return cacheId;
}

From source file:org.ms123.common.data.JdoLayerImpl.java

public Map insertObject(Map dataMap, Map filterMap, Map hintsMap, StoreDesc sdesc, String entityName,
        String entityNameParent, String idParent) {
    debug("insertObject:" + dataMap + ",filterMap:" + filterMap + ",entity:" + entityName + "/entityNameParent:"
            + entityNameParent + "/idParent:" + idParent);
    Map retMap = new HashMap();
    // initialize 
    SessionContext sessionContext = getSessionContext(sdesc);
    UserTransaction ut = m_nucleusService.getUserTransaction();
    try {//from w w w .  j a v  a  2  s  . c om
        ut.begin();
        retMap = insertObject(sessionContext, dataMap, filterMap, hintsMap, entityName, entityNameParent,
                idParent);
        if (retMap.get("constraintViolations") == null) {
            ut.commit();
        } else {
            ut.rollback();
        }
    } catch (Throwable e) {
        sessionContext.handleException(ut, e);
    } finally {
        sessionContext.handleFinally(ut);
    }
    return retMap;
}

From source file:org.ms123.common.data.JdoLayerImpl.java

public Map updateObject(Map dataMap, Map filterMap, Map hintsMap, StoreDesc sdesc, String entityName, String id,
        String entityNameParent, String idParent) {
    m_logger.info("updateObject:" + dataMap + ",filterMap:" + filterMap + ",module:" + entityName);
    debug("updateObject:" + dataMap + ",filterMap:" + filterMap + ",module:" + entityName + ",id:" + id
            + ",moduleNameParent:" + entityNameParent + ",idParent:" + idParent);
    Map retMap = new HashMap();
    SessionContext sessionContext = getSessionContext(sdesc);
    UserTransaction ut = m_nucleusService.getUserTransaction();
    try {/*from w ww  .j  a va  2  s.  c o m*/
        ut.begin();
        retMap = updateObject(sessionContext, dataMap, filterMap, hintsMap, entityName, id, entityNameParent,
                idParent);
        if (retMap.get("constraintViolations") == null) {
            ut.commit();
        } else {
            ut.rollback();
        }
    } catch (Exception e) {
        sessionContext.handleException(ut, e);
    } finally {
        sessionContext.handleFinally(ut);
    }
    return retMap;
}

From source file:org.ms123.common.data.JdoLayerImpl.java

public Map deleteObject(Map dataMap, StoreDesc sdesc, String entityName, String id) {
    info("deleteObject:" + dataMap + ",module:" + entityName);
    debug("deleteObject:" + dataMap + ",module:" + entityName + ",id:" + id);
    Map retMap = new HashMap();
    SessionContext sessionContext = getSessionContext(sdesc);
    UserTransaction ut = m_nucleusService.getUserTransaction();
    try {//from  ww  w . j  av  a 2 s.  c  o  m
        ut.begin();
        retMap = deleteObject(sessionContext, dataMap, entityName, id);
        ut.commit();
    } catch (Exception e) {
        sessionContext.handleException(ut, e);
    } finally {
        sessionContext.handleFinally(ut);
    }
    return retMap;
}

From source file:org.ms123.common.data.quality.QualityBatch.java

protected synchronized List<Map> doCheckFromDb(String state, String id, boolean dry) throws Exception {
    SessionContext sc = getSessionContext();
    m_permittedFields = sc.getPermittedFields(m_entityName, "write");
    init();/*w w w .j av  a  2 s.  c  om*/
    m_count = 0;
    m_dups = 0;
    UserTransaction ut = m_nucleusService.getUserTransaction();
    List<Map> dupList = new ArrayList();
    try {
        ut.begin();
        Class clazz = sc.getClass(getEntityName());
        Extent e = sc.getPM().getExtent(clazz, true);
        String filter = constructFilter(state, id);
        Query q = sc.getPM().newQuery(e, filter);
        q.setFilter(filter);
        List results = (List) q.execute();
        Iterator iter = results.iterator();
        while (iter.hasNext()) {
            Object cand = iter.next();
            Object refObj = compareToOKList(sc, cand, dry);
            if (refObj != null) {
                dupList.add(getOneResult(refObj, cand));
            }
        }
        ut.commit();
    } catch (Throwable e) {
        sc.handleException(ut, e);
    } finally {
        sc.handleFinally(ut);
    }
    info("dupList:" + m_js.deepSerialize(dupList));
    return dupList;
}

From source file:org.ms123.common.data.quality.QualityBatch.java

protected synchronized List<Map> doCheckFromData(List<Map> candidateData) throws Exception {
    SessionContext sc = getSessionContext();
    m_permittedFields = sc.getPermittedFields(m_entityName, "write");
    init();/*  w  w  w.ja v  a2  s  .co m*/
    m_count = 0;
    UserTransaction ut = m_nucleusService.getUserTransaction();
    List<Map> dupList = new ArrayList();
    try {
        ut.begin();
        Iterator<Map> iter = candidateData.iterator();
        while (iter.hasNext()) {
            Map cand = iter.next();
            setRelatedObjects(sc, cand);
            debug("candidateData:" + cand);
            Object refObj = compareToOKList(sc, cand, true);
            if (refObj != null) {
                dupList.add(getOneResult(refObj, cand));
            }
        }
        ut.commit();
    } catch (Throwable e) {
        sc.handleException(ut, e);
    } finally {
        sc.handleFinally(ut);
    }
    return dupList;
}

From source file:org.ms123.common.data.quality.QualityBatch.java

private void train() {
    UserTransaction ut = m_nucleusService.getUserTransaction();
    SessionContext sc = getSessionContext();
    List corbus = new ArrayList();
    try {//from w ww.j  a  va  2  s  .  c om
        ut.begin();
        Class clazz = sc.getClass(getEntityName());
        Extent e = sc.getPM().getExtent(clazz, true);
        Query q = sc.getPM().newQuery(e);
        List results = (List) q.execute();
        Iterator iter = results.iterator();
        List<Map> fields = getFields();
        List<Map> fieldsets = getFieldsets();
        int count = 0;
        while (iter.hasNext()) {
            Object obj = iter.next();
            if ((count++ % 1000) == 1) {
                info("train.num:" + count);
            }
            for (Map field : fields) {
                if (field.get(COMPARE) instanceof FuzzyCompare) {
                    FuzzyCompare fc = (FuzzyCompare) field.get(COMPARE);
                    fc.addTrainValue((String) PropertyUtils.getProperty(obj, (String) field.get(NAME)));
                }
            }
            for (Map fieldset : fieldsets) {
                if (!isEmpty(fieldset.get(EXPRESSION))) {
                    for (Map cmpUnit : (List<Map>) fieldset.get(COMPARE_UNITS)) {
                        Compare c = (Compare) cmpUnit.get(COMPARE);
                        if (c instanceof FuzzyCompare) {
                            FuzzyCompare fc = (FuzzyCompare) c;
                            List fs = (List) cmpUnit.get(FIELDS);
                            String value = getFieldsetValue(obj, fs);
                            fc.addTrainValue(value);
                        }
                    }
                } else {
                    FuzzyCompare fc = (FuzzyCompare) fieldset.get(COMPARE);
                    String value = getFieldsetValue(obj, fieldset);
                    fc.addTrainValue(value);
                }
            }
        }
        ut.commit();
    } catch (Throwable e) {
        sc.handleException(ut, e);
    } finally {
        sc.handleFinally(ut);
    }
}

From source file:org.ms123.common.ea.BaseEAServiceImpl.java

private Map importActivities(String storeId, String basedir) throws Exception {
    String json = readFileToString(new File(basedir, "idmap.map"));
    Map<String, String> idmap = (Map) m_ds.deserialize(json);
    Calendar high_cal = Calendar.getInstance();
    high_cal.set(Calendar.YEAR, 2050);
    high_cal.set(Calendar.MONTH, 11);
    high_cal.set(Calendar.DAY_OF_MONTH, 31);

    Calendar low_cal = Calendar.getInstance();
    low_cal.set(Calendar.YEAR, 2012);
    low_cal.set(Calendar.MONTH, 11);
    low_cal.set(Calendar.DAY_OF_MONTH, 12);
    StoreDesc sdesc = StoreDesc.get(storeId);
    PersistenceManager pm = m_nucleusService.getPersistenceManagerFactory(sdesc).getPersistenceManager();
    UserTransaction ut = m_nucleusService.getUserTransaction();
    Map mapping = initActivities();
    try {// ww  w  . j  a  va  2s. c  o m
        LabeledCSVParser lp = new LabeledCSVParser(
                new ExcelCSVParser(new FileInputStream(new File(basedir, "Kontakte.csv"))));
        System.out.println("Persisting activities");
        int num = 0;
        Class _contact = m_nucleusService.getClass(sdesc, "Contact");
        Class _teamIntern = m_nucleusService.getClass(sdesc, "Teamintern");
        while (lp.getLine() != null) {
            String nummer = lp.getValueByLabel("Nummer");
            String merkmal = lp.getValueByLabel("Merkmal");
            Object c = getObject(pm, _contact, nummer);
            if (c == null) {
                continue;
            }
            if (ut.getStatus() != Status.STATUS_ACTIVE) {
                ut.begin();
            }
            String teamid = idmap.get(merkmal);
            Object activity = m_nucleusService.getClass(sdesc, "Activity").newInstance();
            if (teamid != null) {
                Object team = m_nucleusService.getClass(sdesc, "Team").newInstance();
                Object teamintern = getTeamintern(pm, _teamIntern, teamid);
                PropertyUtils.setProperty(team, "teamintern", teamintern);
                PropertyUtils.setProperty(team, "teamid", teamid);
                PropertyUtils.setProperty(team, "description",
                        PropertyUtils.getProperty(teamintern, "description"));
                PropertyUtils.setProperty(team, "validFrom", low_cal.getTime());
                PropertyUtils.setProperty(team, "validTo", high_cal.getTime());
                PropertyUtils.setProperty(team, "disabled", false);
                Collection l = (Collection) PropertyUtils.getProperty(activity, "_team_list");
                if (l == null) {
                    l = new HashSet();
                    PropertyUtils.setProperty(activity, "_team_list", l);
                }
                l.add(team);
            }
            Iterator it = mapping.keySet().iterator();
            while (it.hasNext()) {
                String key = (String) it.next();
                if (key.equals("traits")) {
                    continue;
                }
                String[] m1 = (String[]) mapping.get(key);
                String field = m1[0];
                String type = m1[1];
                String val = lp.getValueByLabel(key).trim();
                if (type.equals("date")) {
                    Date d = getDate(val);
                    if (d != null) {
                        BeanUtils.setProperty(activity, field, d);
                    }
                } else if (type.equals("boolean")) {
                    Boolean b = false;
                    if ("J".equals(val)) {
                        b = true;
                    }
                    BeanUtils.setProperty(activity, field, b);
                } else {
                    if (val != null) {
                        BeanUtils.setProperty(activity, field, val);
                    }
                }
            }
            Collection l = (Collection) PropertyUtils.getProperty(c, "activity_list");
            l.add(activity);
            PropertyUtils.setProperty(activity, "contact", c);
            pm.makePersistent(activity);
            //LuceneSession luceneSession = m_luceneService.createSession(sdesc);
            //m_luceneService.addToIndex(luceneSession, activity);
            //m_luceneService.commit(luceneSession);
            if ((num % 1000) == 1) {
                System.out.println(num + ":\t" + new Date().getTime());
                ut.commit();
            }
            num++;
        }
        if (ut.getStatus() == Status.STATUS_ACTIVE) {
            ut.commit();
        }
        System.out.println("Contact and Book have been persisted");
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    } finally {
        pm.close();
    }
    return null;
}

From source file:org.ms123.common.ea.BaseEAServiceImpl.java

private Map importTeams(String storeId, String basedir) throws Exception {
    Calendar high_cal = Calendar.getInstance();
    high_cal.set(Calendar.YEAR, 2050);
    high_cal.set(Calendar.MONTH, 11);
    high_cal.set(Calendar.DAY_OF_MONTH, 31);

    Calendar low_cal = Calendar.getInstance();
    low_cal.set(Calendar.YEAR, 2012);
    low_cal.set(Calendar.MONTH, 11);
    low_cal.set(Calendar.DAY_OF_MONTH, 12);

    String json = readFileToString(new File(basedir, "idmap.map"));
    Map<String, String> idmap = (Map) m_ds.deserialize(json);
    StoreDesc sdesc = StoreDesc.get(storeId);
    PersistenceManager pm = m_nucleusService.getPersistenceManagerFactory(sdesc).getPersistenceManager();
    UserTransaction ut = m_nucleusService.getUserTransaction();
    try {//w  w w.j ava  2s.com
        LabeledCSVParser lp = new LabeledCSVParser(
                new ExcelCSVParser(new FileInputStream(new File(basedir, "Merkmale.csv"))));
        System.out.println("Persisting teams");
        int num = 0;
        Class _contact = m_nucleusService.getClass(sdesc, "Contact");
        Class _company = m_nucleusService.getClass(sdesc, "Company");
        Class _teamIntern = m_nucleusService.getClass(sdesc, "Teamintern");
        while (lp.getLine() != null) {
            String nummer = lp.getValueByLabel("Nummer");
            String merkmal = lp.getValueByLabel("Merkmal");
            String beginn = lp.getValueByLabel("Beginn");
            String ende = lp.getValueByLabel("Ende");
            String status = lp.getValueByLabel("Status");
            String teamid = idmap.get(merkmal);
            if (teamid == null) {
                System.out.println("Teamid not found:" + merkmal);
                continue;
            }
            Object c = getObject(pm, _contact, _company, nummer);
            if (c == null) {
                System.out.println("No contact/company:" + nummer);
                continue;
            }
            if (ut.getStatus() != Status.STATUS_ACTIVE) {
                ut.begin();
            }
            Object team = m_nucleusService.getClass(sdesc, "Team").newInstance();
            Object teamintern = getTeamintern(pm, _teamIntern, teamid);
            PropertyUtils.setProperty(team, "teamintern", teamintern);
            PropertyUtils.setProperty(team, "teamid", teamid);
            PropertyUtils.setProperty(team, "description",
                    PropertyUtils.getProperty(teamintern, "description"));

            Boolean active = isActive(status);
            Date validFrom = getTeamDate(beginn);
            Date validTo = getTeamDate(ende);
            if (active != null && validFrom != null && validTo != null) {
                PropertyUtils.setProperty(team, "validFrom", validFrom);
                PropertyUtils.setProperty(team, "validTo", validTo);
                PropertyUtils.setProperty(team, "disabled", !active);
            }
            if (active == null && validFrom == null && validTo == null) {
                PropertyUtils.setProperty(team, "validFrom", low_cal.getTime());
                PropertyUtils.setProperty(team, "validTo", high_cal.getTime());
                PropertyUtils.setProperty(team, "disabled", false);
            }
            if (active != null && validFrom != null && validTo == null) {
                PropertyUtils.setProperty(team, "validFrom", validFrom);
                PropertyUtils.setProperty(team, "validTo", high_cal.getTime());
                PropertyUtils.setProperty(team, "disabled", !active);
            }
            PropertyUtils.setProperty(team, "property1", lp.getValueByLabel("Nutzer"));
            PropertyUtils.setProperty(team, "property2", lp.getValueByLabel("Passwort"));

            Collection l = (Collection) PropertyUtils.getProperty(c, "_team_list");
            if (l == null) {
                l = new HashSet();
                PropertyUtils.setProperty(c, "_team_list", l);
            }
            l.add(team);
            pm.makePersistent(team);
            if ((num % 1000) == 1) {
                System.out.println(num + ":\t" + new Date().getTime());
                ut.commit();
            }
            num++;
        }
        if (ut.getStatus() == Status.STATUS_ACTIVE) {
            ut.commit();
        }
        System.out.println("Contact and Book have been persisted");
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    } finally {
        pm.close();
    }
    return null;
}

From source file:org.ms123.common.ea.BaseEAServiceImpl.java

private Map importCommunications(String storeId, String basedir) {
    StoreDesc sdesc = StoreDesc.get(storeId);
    PersistenceManager pm = m_nucleusService.getPersistenceManagerFactory(sdesc).getPersistenceManager();
    UserTransaction ut = m_nucleusService.getUserTransaction();
    Map mapping = initCommunication();
    try {/* w w w .java  2  s.c  o  m*/
        LabeledCSVParser lp = new LabeledCSVParser(
                new ExcelCSVParser(new FileInputStream(new File(basedir, "Kommunikation.csv"))));
        System.out.println("Persisting communication");
        int num = 0;
        Class _contact = m_nucleusService.getClass(sdesc, "Contact");
        Class _company = m_nucleusService.getClass(sdesc, "Company");
        while (lp.getLine() != null) {
            String nummer = lp.getValueByLabel("Nummer");
            Object c = getObject(pm, _contact, nummer);
            if (c == null) {
                c = getObject(pm, _company, nummer);
                if (c == null) {
                    continue;
                }
            }
            if (ut.getStatus() != Status.STATUS_ACTIVE) {
                ut.begin();
            }
            String typ = lp.getValueByLabel("Typ");
            typ = typ.toLowerCase();
            String adresse = lp.getValueByLabel("Adresse");
            Object comm = PropertyUtils.getProperty(c, "communication");
            if (comm == null) {
                comm = m_nucleusService.getClass(sdesc, "Communication").newInstance();
                PropertyUtils.setProperty(c, "communication", comm);
                pm.makePersistent(comm);
            }
            String[] m1 = (String[]) mapping.get(typ);
            if (m1 == null) {
                System.out.println("typ(" + typ + "): not found");
                continue;
            }
            String field = m1[0];
            BeanUtils.setProperty(comm, field, adresse);
            pm.makePersistent(comm);
            if ((num % 1000) == 1) {
                System.out.println(num + ":\t" + new Date().getTime());
                ut.commit();
            }
            num++;
        }
        if (ut.getStatus() == Status.STATUS_ACTIVE) {
            ut.commit();
        }
        System.out.println("Communication have been persisted");
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    } finally {
        pm.close();
    }
    return null;
}