Java Code Examples for org.hibernate.Session.setDefaultReadOnly()

The following are Jave code examples for showing how to use setDefaultReadOnly() of the org.hibernate.Session class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: sjk   File: DownloadsUpdateServiceImpl.java   View Source Code Vote up 6 votes
@Override
public void updateDownloadsFromMarket() {
    Session session = sessions.openSession();
    try {
        session.setDefaultReadOnly(false);
        long allDownloads = 0;
        List<Market> markets = marketDao.list();
        Map<String, Market> marketMap = new HashMap<String, Market>(4, 1);
        for (Market market : markets) {
            long downloads = marketAppDao.countDownloads(session, market.getMarketName());
            // 设置市场的总下载量
            // market.setDownloads(downloads);
            marketDao.update(session, market);
            allDownloads += downloads;
            marketMap.put(market.getMarketName(), market);
        }
        logger.debug("allDownloads: {}", allDownloads);
        session.flush();
        session.clear();
        logger.info("Update market downloads!");
    } finally {
        session.close();
    }
}
 
Example 2
Project: sjk   File: MergeServiceImpl.java   View Source Code Vote up 6 votes
private Transaction batchIn1TransactionToApp(Session session, List<MarketApp> marketApps) {
    Transaction tx = null;
    try {
        session.setDefaultReadOnly(false);
        tx = session.beginTransaction();
        long start = System.currentTimeMillis();
        mergePaginationToApp(session, marketApps);
        session.flush();
        session.clear();
        logger.info("mergePaginationToApp耗时:{}", (System.currentTimeMillis() - start) + " ms!");
        tx.commit();
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
        logger.error("Exception", e);
    }
    return tx;
}
 
Example 3
Project: sjk   File: DownloadsUpdateServiceImpl.java   View Source Code Vote up 5 votes
@Override
public int handlePagination(UpdateDownloadCallback cb) {
    int effects = 0;
    Session session = sessions.openSession();
    try {
        session.setDefaultReadOnly(false);
        long count = appDao.count(session);
        final int pageSize = AppConfig.BATCH_SIZE_OF_TRANC;
        int totalPage = (int) (count / pageSize + 1);
        int currentPage = 1;
        int index = 0;
        for (; currentPage <= totalPage; currentPage++) {
            List<App> apps = appDao.listForDownloads(session, currentPage, pageSize);
            if (apps == null || apps.isEmpty()) {
                break;
            }
            for (App a : apps) {
                try {
                    effects += cb.doIn(session, a, appDao);
                    if (++index % AppConfig.BATCH_SIZE == 0) {
                        session.flush();
                        session.clear();
                    }
                } catch (Exception e) {
                    session.clear();
                    logger.error("Exception", e);
                }
            }
        }
        session.flush();
        session.clear();
    } finally {
        session.close();
    }
    return effects;
}
 
Example 4
Project: sjk   File: ApiImporterMarketServiceImpl.java   View Source Code Vote up 4 votes
@Override
public void importIncrement() {
    logger.info("{}. ImportIncrement begins...", this.getMarketName());
    Session session = null;
    Transaction tx = null;
    boolean successed = false;
    Date now = new Date();
    do {
        session = sessions.openSession();
        session.setDefaultReadOnly(false);
        try {
            Market market = null;
            try {
                market = getMarket(session);
            } finally {
                session.close();
            }
            int currentPage = 0, totalPage = 0;
            oneIncrement: do {
                PaginationMarketApp increment = getMarketAppForIncrement(market);
                if (increment != null) {
                    currentPage = increment.getCurrentPage();
                    totalPage = increment.getTotalPages();
                    List<MarketApp> marketApps = increment.getData();
                    List<MarketApp> offMarketApps = new ArrayList<MarketApp>();
                    if (marketApps != null && !marketApps.isEmpty()) {
                        // open again
                        if (!session.isOpen()) {
                            session = sessions.openSession();
                        }
                        tx = session.beginTransaction();
                        savePaginationMarketApp(session, market, marketApps, offMarketApps);
                        market.setIncrementLastReqCurrentPage(currentPage);
                        market.setIncrementTotalPages(totalPage);
                        session.merge(market);
                        tx.commit();
                        tx = null;
                        session.close();
                        deleteMarketAppsTransaction(offMarketApps);
                        logger.info(
                                "{}. Increment totalPage: {}\t currentPage: {}\t size: {}\t offMarketApps size: {}",
                                this.getMarketName(), totalPage, currentPage, marketApps.size(),
                                offMarketApps.size());
                        marketApps.clear();
                        offMarketApps.clear();
                    }
                    if (currentPage >= totalPage) {
                        successed = true;
                    }
                } else {
                    successed = false;
                    break oneIncrement;
                }
            } while (totalPage > 0 && currentPage < totalPage);
            if (successed) {
                resetMarketForIncrement(now);
            } else {
                sleepForTry();
            }
        } catch (Exception e) {
            successed = false;
            if (tx != null) {
                tx.rollback();
            }
            logger.error("Exception", e);
            sleepForTry();
        } finally {
            if (session.isOpen()) {
                session.close();
            }
        }
    } while (!successed);
    logger.info("{}. ImportIncrement done!", this.getMarketName());
}
 
Example 5
Project: sjk   File: ApiImporterMarketServiceImpl.java   View Source Code Vote up 4 votes
private PaginationMarketApp savePaginationForFull() throws Exception {
    // saveOrUpdate to table MarketApp
    PaginationMarketApp marketPagination = null;
    Session session = sessions.openSession();
    session.setDefaultReadOnly(false);
    Transaction tx = null;
    try {
        Market market = getMarket(session);
        marketPagination = getMarketAppForFull(market);
        if (marketPagination != null && marketPagination.getTotalPages() >= marketPagination.getCurrentPage()) {
            List<MarketApp> marketApps = marketPagination.getData();
            List<MarketApp> offMarketApps = new ArrayList<MarketApp>();
            if (marketApps != null && !marketApps.isEmpty()) {
                Object[] infos = new Object[] { market.getMarketName(), marketPagination.getTotalPages(),
                        marketPagination.getCurrentPage(), marketApps.size() };
                accessMarketDao.getMarketlogger().info(
                        "Begin to import {} on one page. totalPages: {} currentPage : {} , size: {}", infos);
                tx = session.beginTransaction();
                savePaginationMarketApp(session, market, marketApps, offMarketApps);
                market.setFullLastReqCurrentPage(marketPagination.getCurrentPage());
                market.setFullTotalPages(marketPagination.getTotalPages());
                market.setFullLastTime(new Date());
                session.merge(market);
                tx.commit();
                tx = null;
                session.clear();
                session.close();
                deleteMarketAppsTransaction(offMarketApps);
                marketApps.clear();
                offMarketApps.clear();
            }
        }
    } catch (Exception e) {
        if (tx != null) {
            tx.rollback();
        }
        logger.error("Exception", e);
        throw e;
    } finally {
        if (session.isOpen()) {
            session.close();
        }
    }
    return marketPagination;
}
 
Example 6
Project: sjk   File: MergeServiceImpl.java   View Source Code Vote up 4 votes
/**
 * 特殊逻辑处理,处理合作市场
 * 
 * @param forceMerge
 * @param mApp
 * @param session
 * @param now
 * @param updateApp
 */
private void updateAppFromMarketApp(boolean forceMerge, MarketApp mApp, Session session, Date now, App updateApp) {
    // channel apk!!!
    // if (!mApp.getMarketName().equals(updateApp.getMarketName())) {
    // switch (EnumMarket.valueOf(updateApp.getMarketName().toUpperCase()))
    // {
    // case SHOUJIKONG_CHANNEL: {
    // // update appfetchtime
    // mApp.setAppFetchTime(now);
    // marketAppDao.update(session, mApp);
    // return;
    // }
    // default: {
    // break;
    // }
    // }
    // }

    if (appConfig.isUpdateAudit()) {
        if (updateApp.isAudit() && !updateApp.isAutoCover()) {
            // update appfetchtime
            mApp.setAppFetchTime(now);
            marketAppDao.update(session, mApp);
            return;
        }
    }

    Session sessionForSelect = sessions.openSession();
    // marketapp 所属的market
    // 需要更新的app 所属的market
    Market market = null, appWithSignatureOwnerMarket;
    try {
        sessionForSelect.setDefaultReadOnly(true);
        market = marketDao.getByName(sessionForSelect, mApp.getMarketName());
        appWithSignatureOwnerMarket = updateApp.getMarketName().equals(mApp.getMarketName()) ? market : marketDao
                .getByName(sessionForSelect, updateApp.getMarketName());
    } finally {
        sessionForSelect.close();
    }

    // channel apk!!!
    // switch (EnumMarket.valueOf(mApp.getMarketName().toUpperCase())) {
    // case SHOUJIKONG_CHANNEL: {
    // // update appfetchtime
    // forceMerge = true;
    // break;
    // }
    // default: {
    // break;
    // }
    // }

    // 有签名
    if (forceMerge || mergeAppComparator.compare(mApp, updateApp, market, appWithSignatureOwnerMarket) > 0
            || StringUtils.isBlank(updateApp.getSignatureSha1())) {
        updateAppIfNeedUpdate(mApp, market, session, now, updateApp);
    }
    // finally set fetchTime
    // update appfetchtime
    mApp.setAppFetchTime(now);
    marketAppDao.update(session, mApp);
}