Example usage for org.hibernate StatelessSession close

List of usage examples for org.hibernate StatelessSession close

Introduction

In this page you can find the example usage for org.hibernate StatelessSession close.

Prototype

void close();

Source Link

Document

Close the stateless session and release the JDBC connection.

Usage

From source file:edu.psu.iam.cpr.batch.processor.impl.HMCBatchProcessor.java

License:Creative Commons License

/**
 * This method provides the implementation of the Hershey medical center processor.
 * @param databaseSession contains an instance of the database session.
 * @param messagingCore contains the messaging core instance.
 * @param dataQualityService contains an instance to the data flux server.
 * @throws CprException will be thrown if there are any Cpr relate problems.
 *//* w  w  w  . j a v a  2s  .c  o  m*/
@Override
public void implementBatchProcessor(final StatelessSession databaseSession, final MessagingCore messagingCore,
        final DataQualityService dataQualityService) throws CprException, JSONException, JMSException {

    final long startTime, stopTime;
    final long totalRecords;
    long recordsProcessed = 0;

    startTime = System.currentTimeMillis();

    // Perform a query for all of the trans hershey records.
    final Query query = databaseSession
            .createQuery("FROM " + TRANS_DATABASE_TABLE + " ORDER BY transHersheyKey ASC");
    query.setFetchSize(RECORD_FETCH_SIZE);
    // Below line should be uncommented during manual testing to only allow a certain number of records through at a time.
    //      query.setMaxResults(TEST_RECORD_MAX_SIZE);

    StatelessSession recordSession = SessionFactoryUtil.getSessionFactory().openStatelessSession();
    final EmployeeInfo employeeInfo = new EmployeeInfo(recordSession, BATCH_DATA_SOURCE, dataQualityService);
    final ChangeNotification changeNotification = new ChangeNotification(recordSession, messagingCore);
    affiliationCalculator = new EduPersonAffiliationCalculator(recordSession, BATCH_DATA_SOURCE,
            dataQualityService);

    Transaction tx = null;

    final List<?> queryList = query.list();
    totalRecords = queryList.size();

    // Loop for all of the records that were found.
    for (final Iterator<?> it = queryList.iterator(); it.hasNext();) {
        final TransHershey transHershey = (TransHershey) it.next();
        recordsProcessed++;

        try {
            // Begin transaction.
            tx = recordSession.beginTransaction();

            employeeInfo.resetHistoryBeans();
            affiliationCalculator.resetHistoryBeans();

            employeeInfo.setRecordNumber(transHershey.getTransHersheyKey().longValue());
            employeeInfo.findPerson(transHershey.getPsuId());
            final Long personId = employeeInfo.getPersonId();
            if (personId == null) {

                // The psu id might have been merged or reassigned.
                // Call CIDR to check. If so, do an update.

                addEmployee(transHershey, employeeInfo, changeNotification);
            } else {
                updateEmployee(transHershey, employeeInfo, changeNotification);
            }

            // Commit!
            tx.commit();
        } catch (final HibernateException e) { // $codepro.audit.disable logExceptions

            // Log the error.
            LOG.error(BATCH_DATA_SOURCE.toString() + " Batch: error encountered on record #: "
                    + transHershey.getTransHersheyKey(), e);

            // Rollback the transaction, close the session.
            tx.rollback();
            recordSession.close();

            // We need to create a new session and update the helper classes with the new session.
            recordSession = SessionFactoryUtil.getSessionFactory().openStatelessSession();
            employeeInfo.setDatabaseSession(recordSession);
            changeNotification.setStatelessSession(recordSession);
            affiliationCalculator.setDatabaseSession(recordSession);
        }
    }

    try {
        recordSession.close();
    } catch (final HibernateException e) { // $codepro.audit.disable logExceptions

        // Rollback the transaction, close the session.
        tx.rollback();
        recordSession.close();

        // We need to create a new session and update the helper classes with the new session.
        recordSession = SessionFactoryUtil.getSessionFactory().openStatelessSession();
        employeeInfo.setDatabaseSession(recordSession);
        changeNotification.setStatelessSession(recordSession);
        affiliationCalculator.setDatabaseSession(recordSession);
    }

    stopTime = System.currentTimeMillis();
    final double elapsedTime = ((double) stopTime - startTime) / 1000;

    LOG.info(BATCH_DATA_SOURCE.toString() + " Batch: processed " + recordsProcessed + " records out of "
            + totalRecords + " in " + elapsedTime + " seconds");
}

From source file:edu.psu.iam.cpr.batch.processor.impl.NamesBatchProcessor.java

License:Creative Commons License

/**
 * This method implements the core logic of the batch processor.
 * @param databaseSession contains the database session from the abstract class that will be used to iterator over the individual records.
 * @param messagingCore contains a reference to the messaging infrastructure.
 * @param dataQualityService contains a reference to the data quality service.
 * @throws CprException will be thrown for any Cpr Related problems.
 * @throws JSONException will be thrown for any JSON problems.
 * @throws JMSException will be thrown for any messaging problems.
 *///from  w ww. ja v a  2  s .  com
@Override
public void implementBatchProcessor(StatelessSession databaseSession, MessagingCore messagingCore,
        DataQualityService dataQualityService)
        throws CprException, ParseException, JSONException, JMSException {

    final Date d = new Date();
    final long startTime, stopTime;
    final long totalRecords;
    long recordsProcessed = 0;

    startTime = System.currentTimeMillis();

    // Calculate the start and end date/times based on the current date.
    setStartDateTime(Utility.makeStartDate(d));
    setEndDateTime(Utility.makeEndDate(d));

    // Do a select to find all of the people who have had a name changed for the current date.
    final Query query = databaseSession.createQuery(
            "select distinct personId from NamesStaging where importDate BETWEEN :start_date AND :end_date");
    query.setFetchSize(RECORD_FETCH_SIZE);
    query.setParameter("start_date", startDateTime);
    query.setParameter("end_date", endDateTime);
    totalRecords = query.list().size();

    // Init some objects.
    StatelessSession recordSession = SessionFactoryUtil.getSessionFactory().openStatelessSession();
    final NamesPostProcessor namesPostProcessor = new NamesPostProcessor(recordSession, messagingCore);
    final ChangeNotification changeNotification = new ChangeNotification(recordSession, messagingCore);
    final PersonBio personBio = new PersonBio(recordSession, BatchDataSource.NAME_POSTPROCESS,
            dataQualityService);
    Transaction tx = null;

    // Loop through the results.
    for (final Iterator<?> it = query.list().iterator(); it.hasNext();) {
        Long personId = (Long) it.next();

        try {

            tx = recordSession.beginTransaction();

            // Process a name change for a person.
            namesPostProcessor.resetHistoryBeans();
            namesPostProcessor.processNameChange(personId);

            // Only check for messaging if there was a change.
            if (namesPostProcessor.getOldNamesBean() != null || namesPostProcessor.getNewNamesBean() != null) {

                // Find the person using their person identifier.
                Long pid = personBio.findPersonUsingPersonId(personId);
                if (pid != null && pid.equals(personId)) {

                    // Set the required information for a json message.
                    changeNotification.setRequiredInfo(personId, personBio.getPsuIdNumber(),
                            personBio.getPrimaryUserid(), namesPostProcessor.getNewNamesBean().getImportFrom());

                    // Name change.
                    changeNotification.nameChange(namesPostProcessor.getOldNamesBean(),
                            namesPostProcessor.getNewNamesBean());
                }
            }

            tx.commit();
            recordsProcessed++;

        } catch (HibernateException e) {
            LOG.info("Names Post Processor Batch: error encountered person identifier : " + personId);
            tx.rollback();
            recordSession.close();
            recordSession = SessionFactoryUtil.getSessionFactory().openStatelessSession();
            namesPostProcessor.setStatelessSession(recordSession);
            changeNotification.setStatelessSession(recordSession);
            personBio.setDatabaseSession(recordSession);
        }
    }

    try {
        recordSession.close();
    } catch (Exception e) { // $codepro.audit.disable emptyCatchClause
    }

    stopTime = System.currentTimeMillis();
    final double elapsedTime = ((double) stopTime - startTime) / 1000;

    LOG.info(BatchDataSource.NAME_POSTPROCESS.toString() + " Batch: processed " + recordsProcessed
            + " records out of " + totalRecords + " in " + elapsedTime + " seconds");

}

From source file:edu.psu.iam.cpr.batch.processor.impl.OasisBatchProcessor.java

License:Creative Commons License

/**
 * This method implements the core logic of the batch processor.
 * @param databaseSession contains the database session from the abstract class that will be used to iterator over the individual records.
 * @param messagingCore contains a reference to the messaging infrastructure.
 * @param dataQualityService contains a reference to the data quality service.
 * @throws CprException will be thrown for any Cpr Related problems.
 * @throws JSONException will be thrown for any JSON problems.
 * @throws JMSException will be thrown for any messaging problems.
 *//*w  w  w  .  j  a va2  s.  c  om*/
@Override
public void implementBatchProcessor(final StatelessSession databaseSession, final MessagingCore messagingCore,
        final DataQualityService dataQualityService) throws CprException, JSONException, JMSException {

    final long startTime, stopTime;
    final long totalRecords;
    long recordsProcessed = 0;

    startTime = System.currentTimeMillis();

    // Perform a query for all of the trans oasis records.
    final Query query = databaseSession.createQuery("from " + TRANS_DATABASE_TABLE);
    query.setFetchSize(RECORD_FETCH_SIZE);

    StatelessSession recordSession = SessionFactoryUtil.getSessionFactory().openStatelessSession();
    final PersonBio personBio = new PersonBio(recordSession, BATCH_DATA_SOURCE, dataQualityService);
    final AccessAccountStatus accessAccountStatus = new AccessAccountStatus(recordSession, messagingCore);
    final ChangeNotification changeNotification = new ChangeNotification(recordSession, messagingCore);
    Transaction tx = null;

    final List<?> queryList = query.list();
    totalRecords = queryList.size();

    // Loop for all of the records that were found.
    for (final Iterator<?> it = queryList.iterator(); it.hasNext();) {
        final TransOasis transOasis = (TransOasis) it.next();
        recordsProcessed++;
        try {
            // Begin transaction.
            tx = recordSession.beginTransaction();

            // Using person bio, find the person and their associated userid.  NOTE: if the userid is not in the userid table, person bio will add it.
            personBio.resetHistoryBeans();
            personBio.findPerson(transOasis.getPsuId());
            final Long personId = personBio.getPersonId();
            if (personId == null) {
                LOG.info(BATCH_DATA_SOURCE.toString()
                        + " Batch: psu id number not found error encountered on record #: "
                        + transOasis.getTransOasisKey());
            } else {
                final String userid = transOasis.getUserid().toLowerCase();
                personBio.updateUserid(userid);

                final String status = transOasis.getStatus();

                // Update the status.
                accessAccountStatus.processStatusChange(personId, userid, AccessAccountStatusType.get(status));

                if (personBio.getPrimaryUserid() == null && personBio.getNewUserid() != null) {
                    personBio.setPrimaryUserid(personBio.getNewUserid().getUserid());
                }

                changeNotification.setRequiredInfo(personId, personBio.getPsuIdNumber(),
                        personBio.getPrimaryUserid(), BATCH_DATA_SOURCE.toString());

                changeNotification.useridChange(personBio.getOldUserid(), personBio.getNewUserid());

                if (accessAccountStatus.getOldAccessAccountStatus() != accessAccountStatus
                        .getNewAccessAccountStatus()) {
                    changeNotification.accountStatusChange(accessAccountStatus.getOldAccessAccountStatus(),
                            accessAccountStatus.getNewAccessAccountStatus());
                }
            }

            // Commit!
            tx.commit();
        } catch (final HibernateException e) { // $codepro.audit.disable logExceptions

            // Log the error.
            LOG.error(BATCH_DATA_SOURCE.toString() + " Batch: error encountered on record #: "
                    + transOasis.getTransOasisKey());

            // Rollback the transaction, close the session.
            tx.rollback();
            recordSession.close();

            // We need to create a new session and update the person bio and access account status classes with the new session.
            recordSession = SessionFactoryUtil.getSessionFactory().openStatelessSession();
            personBio.setDatabaseSession(recordSession);
            accessAccountStatus.setDatabaseSession(recordSession);
            changeNotification.setStatelessSession(recordSession);
        } catch (final CprException ex) {

            // Log the error.
            LOG.error(BATCH_DATA_SOURCE.toString() + " Batch: error encountered on record #: "
                    + transOasis.getTransOasisKey(), ex);

            // Rollback the transaction, close the session.
            tx.rollback();
            recordSession.close();

            // We need to create a new session and update the person bio and access account status classes with the new session.
            recordSession = SessionFactoryUtil.getSessionFactory().openStatelessSession();
            personBio.setDatabaseSession(recordSession);
            accessAccountStatus.setDatabaseSession(recordSession);
            changeNotification.setStatelessSession(recordSession);
        }
    }

    try {
        recordSession.close();
    } catch (final HibernateException e) { // $codepro.audit.disable emptyCatchClause, logExceptions
    }

    stopTime = System.currentTimeMillis();
    final double elapsedTime = ((double) stopTime - startTime) / 1000;

    LOG.info(BATCH_DATA_SOURCE.toString() + " Batch: processed " + recordsProcessed + " records out of "
            + totalRecords + " in " + elapsedTime + " seconds");
}

From source file:edu.psu.iam.cpr.batch.processor.impl.StudentBatchProcessor.java

License:Creative Commons License

/**
 * This method provides the implementation of the student processor.
 * @param databaseSession contains an instance of the database session.
 * @param messagingCore contains the messaging core instance.
 * @param dataQualityService contains a reference to the data quality service.
 * @throws CprException will be thrown for any Cpr Related problems.
 * @throws JSONException will be thrown for any JSON problems.
 * @throws JMSException will be thrown for any messaging problems.
 *///from www  .  j av  a2  s .c om
@Override
public void implementBatchProcessor(final StatelessSession databaseSession, final MessagingCore messagingCore,
        final DataQualityService dataQualityService)
        throws CprException, ParseException, JSONException, JMSException {

    final long startTime, stopTime;
    final long totalRecords;
    long recordsProcessed = 0;

    startTime = System.currentTimeMillis();

    String currentSemester = null;
    // Get the current semester
    final Date d = new Date();
    Query query = databaseSession
            .createQuery("from Semesters where :now >= semStartDate AND :now < semEndDate");
    query.setParameter(NOW, d);

    for (final Iterator<?> it = query.list().iterator(); it.hasNext();) {
        final Semesters bean = (Semesters) it.next();
        currentSemester = bean.getSemesterCode();
    }

    // Perform a query for all of the trans stubio records.
    query = databaseSession
            .createQuery("from " + TRANS_DATABASE_TABLE + " order by sem asc, codeStudStat desc");
    query.setFetchSize(RECORD_FETCH_SIZE);

    StatelessSession recordSession = SessionFactoryUtil.getSessionFactory().openStatelessSession();
    final StudentInfo studentInfo = new StudentInfo(recordSession, BATCH_DATA_SOURCE, dataQualityService);
    final EduPersonAffiliationCalculator eduPersonAffiliationCalculator = new EduPersonAffiliationCalculator(
            recordSession, BATCH_DATA_SOURCE, dataQualityService);
    final ChangeNotification changeNotification = new ChangeNotification(recordSession, messagingCore);
    Transaction tx = null;

    final List<?> queryList = query.list();
    totalRecords = queryList.size();

    // Loop for all of the records that were found.
    for (final Iterator<?> it = queryList.iterator(); it.hasNext();) {
        final TransStubio transStubio = (TransStubio) it.next();
        recordsProcessed++;
        try {
            final String semesterCode = transStubio.getSem();
            // Begin transaction.
            tx = recordSession.beginTransaction();

            studentInfo.resetHistoryBeans();

            Long personId = studentInfo.findPerson(transStubio.getPsuId());
            if (personId == null && transStubio.getPsuIdPrev() != null) {
                personId = studentInfo.findPerson(transStubio.getPsuIdPrev());

                // Update PSU ID
                if (personId != null) {
                    studentInfo.updatePsuId(transStubio.getPsuId());
                }
            }

            // User was not found, so we need to add them.
            if (personId == null) {

                // Create a new person
                studentInfo.addPerson();

                // Add their PSU ID
                studentInfo.addPsuId(transStubio.getPsuId());

                // Add Date of birth
                studentInfo.addDateOfBirth(parseDateString(transStubio.getDatePersBirth()));

                // Add Gender if not null
                if (!ValidateHelper.isFieldEmpty(transStubio.getCodePersSex())) {
                    GenderType genderType;
                    try {
                        genderType = Utility.genderStringToType(transStubio.getCodePersSex());
                        studentInfo.addGender(genderType);
                    } catch (final IllegalArgumentException e) {
                        LOG.error("Invalid gender for record " + transStubio.getTransStubioKey());
                    }

                }

                // Update phones
                studentInfo.updatePhone(PhoneType.LOCAL_PHONE, transStubio.getPhoneLocal(), null);
                final Phones newPhone = studentInfo.getNewPhone();
                studentInfo.updatePhone(PhoneType.PERMANENT_PHONE, transStubio.getPhoneHome(), null);

                // Add name
                studentInfo.addName(transStubio.getNamePersFirst(), transStubio.getNamePersMid(),
                        transStubio.getNamePersLast(), transStubio.getNamePersSfx());

                // Update addresses
                studentInfo.updateAddress(AddressType.LOCAL_ADDRESS, transStubio.getAddrLoclSt1(),
                        transStubio.getAddrLoclSt2(), transStubio.getAddrLoclSt3(),
                        transStubio.getAddrLoclCity(), transStubio.getAddrLoclState(),
                        transStubio.getAddrLoclZip(), transStubio.getAddrLoclCtry(), transStubio.getCodeCamp());
                final Addresses newAddress = studentInfo.getNewAddress();

                studentInfo.updateAddress(AddressType.PERMANENT_ADDRESS, transStubio.getAddrHomeSt1(),
                        transStubio.getAddrHomeSt2(), transStubio.getAddrHomeSt3(),
                        transStubio.getAddrHomeCity(), transStubio.getAddrHomeState(),
                        transStubio.getAddrHomeZip(), transStubio.getAddrHomeCtry(), null);

                // Add the student record
                if (transStubio.getCodeStudStat() != null && transStubio.getCodeStudLvl() != null) {
                    studentInfo.addStudent(semesterCode, transStubio.getCodeCamp(),
                            transStubio.getCodeStudStat(), transStubio.getCodeStudLvl(),
                            transStubio.getCodeStudClsfctnYrtm(), transStubio.getIndcStudGraduation(),
                            transStubio.getLoaStart(), transStubio.getLoaReturnSt1(), transStubio.getCodeHotl(),
                            transStubio.getClassLoad(), transStubio.getStudentAid());

                    // Add the student's academic colleges
                    studentInfo.addStudentAcademicCollege(semesterCode, transStubio.getCodeStudColl1(),
                            LONG_ONE);
                    final StudentAcademicCollege newStudentAcademicCollege = studentInfo
                            .getNewStudentAcademicCollege();
                    studentInfo.addStudentAcademicCollege(semesterCode, transStubio.getCodeStudColl2(),
                            LONG_TWO);
                    studentInfo.addStudentAcademicCollege(semesterCode, transStubio.getCodeStudColl3(),
                            LONG_THREE);
                    studentInfo.addStudentAcademicCollege(semesterCode, transStubio.getCodeStudColl4(),
                            LONG_FOUR);

                    // Update the student's academic departments
                    studentInfo.addStudentAcademicDepartment(semesterCode, transStubio.getCodeStudAcdt1(),
                            LONG_ONE);
                    final StudentAcademicDepartment newStudentAcademicDepartment = studentInfo
                            .getNewStudentAcademicDepartment();
                    studentInfo.addStudentAcademicDepartment(semesterCode, transStubio.getCodeStudAcdt2(),
                            LONG_TWO);
                    studentInfo.addStudentAcademicDepartment(semesterCode, transStubio.getCodeStudAcdt3(),
                            LONG_THREE);
                    studentInfo.addStudentAcademicDepartment(semesterCode, transStubio.getCodeStudAcdt4(),
                            LONG_FOUR);

                    // Update the student's majors
                    studentInfo.addStudentMajor(semesterCode, transStubio.getCodeStudMajr1(), LONG_ONE);
                    final StudentMajor newStudentMajor = studentInfo.getNewStudentMajor();
                    studentInfo.addStudentMajor(semesterCode, transStubio.getCodeStudMajr2(), LONG_TWO);
                    studentInfo.addStudentMajor(semesterCode, transStubio.getCodeStudMajr3(), LONG_THREE);
                    studentInfo.addStudentMajor(semesterCode, transStubio.getCodeStudMajr4(), LONG_FOUR);

                    changeNotification.setRequiredInfo(studentInfo.getPersonId(), studentInfo.getPsuIdNumber(),
                            null, BATCH_DATA_SOURCE.toString());
                    if (Utility.areStringFieldsEqual(currentSemester, semesterCode)) {
                        eduPersonAffiliationCalculator.setPersonId(studentInfo.getPersonId());
                        eduPersonAffiliationCalculator.setStudentAffiliation(transStubio.getCodeStudStat());
                        changeNotification.primaryAffiliationChange(
                                eduPersonAffiliationCalculator.getOldPersonAffiliation(),
                                eduPersonAffiliationCalculator.getNewPersonAffiliation());
                    }
                    changeNotification.newStudent(studentInfo.getNewName(), newAddress, newPhone, null, // email address.
                            eduPersonAffiliationCalculator.getNewPersonAffiliation(), // affiliation.
                            studentInfo.getNewPersonGender(), null, // confidentiality
                            studentInfo.getNewStudent(), newStudentAcademicCollege,
                            newStudentAcademicDepartment, newStudentMajor);
                } else {
                    LOG.error("Skipping student record: " + transStubio.getTransStubioKey()
                            + " due to a lack of data.");
                }

            }

            // User was found.
            else {

                changeNotification.setRequiredInfo(studentInfo.getPersonId(), studentInfo.getPsuIdNumber(),
                        studentInfo.getPrimaryUserid(), BATCH_DATA_SOURCE.toString());

                // Update name - change name for name will be done in the names post processor.
                studentInfo.updateName(transStubio.getNamePersFirst(), transStubio.getNamePersMid(),
                        transStubio.getNamePersLast(), transStubio.getNamePersSfx());

                // Update addresses
                studentInfo.updateAddress(AddressType.LOCAL_ADDRESS, transStubio.getAddrLoclSt1(),
                        transStubio.getAddrLoclSt2(), transStubio.getAddrLoclSt3(),
                        transStubio.getAddrLoclCity(), transStubio.getAddrLoclState(),
                        transStubio.getAddrLoclZip(), transStubio.getAddrLoclCtry(), transStubio.getCodeCamp());
                changeNotification.addressChange(studentInfo.getOldAddress(), studentInfo.getNewAddress());

                studentInfo.updateAddress(AddressType.PERMANENT_ADDRESS, transStubio.getAddrHomeSt1(),
                        transStubio.getAddrHomeSt2(), transStubio.getAddrHomeSt3(),
                        transStubio.getAddrHomeCity(), transStubio.getAddrHomeState(),
                        transStubio.getAddrHomeZip(), transStubio.getAddrHomeCtry(), null);

                // Update Date of birth
                studentInfo.updateDateOfBirth(parseDateString(transStubio.getDatePersBirth()));
                changeNotification.dateOfBirthChange(studentInfo.getOldDateOfBirth(),
                        studentInfo.getNewDateOfBirth());
                //TODO Add change notifications for date of birth.

                // Update Gender if not null
                if (!ValidateHelper.isFieldEmpty(transStubio.getCodePersSex())) {
                    try {
                        final GenderType genderType = Utility.genderStringToType(transStubio.getCodePersSex());
                        studentInfo.updateGender(genderType);
                        changeNotification.genderChange(studentInfo.getOldPersonGender(),
                                studentInfo.getNewPersonGender());
                    } catch (final IllegalArgumentException e) {
                        LOG.error("Invalid gender for record " + transStubio.getTransStubioKey());
                    }
                }

                // Update phones
                studentInfo.updatePhone(PhoneType.LOCAL_PHONE, transStubio.getPhoneLocal(), null);
                changeNotification.phoneChange(studentInfo.getOldPhone(), studentInfo.getNewPhone());

                studentInfo.updatePhone(PhoneType.PERMANENT_PHONE, transStubio.getPhoneHome(), null);

                boolean processRecord = true;

                // If the student status and academic level are null, we need to do an additional check.
                if (transStubio.getCodeStudStat() == null && transStubio.getCodeStudLvl() == null) {

                    // Pull the number of records for the current user and their semester code.
                    final String sqlQuery = "from TransStubio where psuId = :psu_id AND sem = :sem";
                    final Query countQuery = recordSession.createQuery(sqlQuery);
                    countQuery.setParameter("psu_id", transStubio.getPsuId());
                    countQuery.setParameter("sem", transStubio.getSem());

                    // If there are more than one record, we need to skip this one because it contains null information.  Based
                    // on analysis there will be another record in the file that will contain real data.  This null record must
                    // be skipping.
                    if (countQuery.list().size() > 1) {
                        processRecord = false;
                    }
                }

                // Check to see if we are going to process the record...
                if (processRecord) {
                    studentInfo.updateStudent(semesterCode, transStubio.getCodeCamp(),
                            transStubio.getCodeStudStat(), transStubio.getCodeStudLvl(),
                            transStubio.getCodeStudClsfctnYrtm(), transStubio.getIndcStudGraduation(),
                            transStubio.getLoaStart(), transStubio.getLoaReturnSt1(), transStubio.getCodeHotl(),
                            transStubio.getClassLoad(), transStubio.getStudentAid());

                    // Update the student's academic colleges
                    studentInfo.updateStudentAcademicCollege(semesterCode, transStubio.getCodeStudColl1(),
                            LONG_ONE);
                    final StudentAcademicCollege oldStudentAcademicCollege = studentInfo
                            .getOldStudentAcademicCollege();
                    final StudentAcademicCollege newStudentAcademicCollege = studentInfo
                            .getNewStudentAcademicCollege();
                    studentInfo.updateStudentAcademicCollege(semesterCode, transStubio.getCodeStudColl2(),
                            LONG_TWO);
                    studentInfo.updateStudentAcademicCollege(semesterCode, transStubio.getCodeStudColl3(),
                            LONG_THREE);
                    studentInfo.updateStudentAcademicCollege(semesterCode, transStubio.getCodeStudColl4(),
                            LONG_FOUR);

                    // Update the student's academic departments
                    studentInfo.updateStudentAcademicDepartment(semesterCode, transStubio.getCodeStudAcdt1(),
                            LONG_ONE);
                    final StudentAcademicDepartment oldStudentAcademicDepartment = studentInfo
                            .getOldStudentAcademicDepartment();
                    final StudentAcademicDepartment newStudentAcademicDepartment = studentInfo
                            .getNewStudentAcademicDepartment();
                    studentInfo.updateStudentAcademicDepartment(semesterCode, transStubio.getCodeStudAcdt2(),
                            LONG_TWO);
                    studentInfo.updateStudentAcademicDepartment(semesterCode, transStubio.getCodeStudAcdt3(),
                            LONG_THREE);
                    studentInfo.updateStudentAcademicDepartment(semesterCode, transStubio.getCodeStudAcdt4(),
                            LONG_FOUR);

                    // Update the student's majors
                    studentInfo.updateStudentMajor(semesterCode, transStubio.getCodeStudMajr1(), LONG_ONE);
                    final StudentMajor oldStudentMajor = studentInfo.getOldStudentMajor();
                    final StudentMajor newStudentMajor = studentInfo.getNewStudentMajor();
                    studentInfo.updateStudentMajor(semesterCode, transStubio.getCodeStudMajr2(), LONG_TWO);
                    studentInfo.updateStudentMajor(semesterCode, transStubio.getCodeStudMajr3(), LONG_THREE);
                    studentInfo.updateStudentMajor(semesterCode, transStubio.getCodeStudMajr4(), LONG_FOUR);

                    // Change notification for student data.
                    changeNotification.studentChange(studentInfo.getOldStudent(), oldStudentAcademicCollege,
                            oldStudentAcademicDepartment, oldStudentMajor, studentInfo.getNewStudent(),
                            newStudentAcademicCollege, newStudentAcademicDepartment, newStudentMajor);

                    // If we are looking at the current semester
                    // process the Affiliation Change.
                    if (Utility.areStringFieldsEqual(currentSemester, semesterCode)) {
                        eduPersonAffiliationCalculator.setPersonId(studentInfo.getPersonId());
                        eduPersonAffiliationCalculator.setStudentAffiliation(transStubio.getCodeStudStat());
                        changeNotification.primaryAffiliationChange(
                                eduPersonAffiliationCalculator.getOldPersonAffiliation(),
                                eduPersonAffiliationCalculator.getNewPersonAffiliation());
                    }

                } else {
                    LOG.error("Skipping student record: " + transStubio.getTransStubioKey()
                            + " due to a lack of data.");
                }
            }

            // Commit!
            tx.commit();
        } catch (final HibernateException e) {

            // Log the error.
            LOG.error(BATCH_DATA_SOURCE.toString() + " Batch: error encountered on record #: "
                    + transStubio.getTransStubioKey(), e);

            // Rollback the transaction, close the session.
            tx.rollback();
            recordSession.close();

            // We need to create a new session and update the person bio with the new session.
            recordSession = SessionFactoryUtil.getSessionFactory().openStatelessSession();
            studentInfo.setDatabaseSession(recordSession);
            changeNotification.setStatelessSession(recordSession);
            eduPersonAffiliationCalculator.setDatabaseSession(recordSession);
        } catch (final CprException ex) {
            LOG.error(BATCH_DATA_SOURCE.toString() + " Batch: error encountered on record #: "
                    + transStubio.getTransStubioKey(), ex);
            throw ex;
        } catch (final RuntimeException ex) {
            // Log the error.
            LOG.error(BATCH_DATA_SOURCE.toString() + " Batch: error encountered on record #: "
                    + transStubio.getTransStubioKey(), ex);

            // Rollback the transaction, close the session.
            tx.rollback();
            recordSession.close();

            // We need to create a new session and update the person bio with the new session.
            recordSession = SessionFactoryUtil.getSessionFactory().openStatelessSession();
            studentInfo.setDatabaseSession(recordSession);
            changeNotification.setStatelessSession(recordSession);
        }
    }

    try {
        recordSession.close();
    } catch (final HibernateException e) { // $codepro.audit.disable logExceptions, emptyCatchClause
    }

    stopTime = System.currentTimeMillis();
    final double elapsedTime = ((double) stopTime - startTime) / 1000;

    LOG.info(BATCH_DATA_SOURCE.toString() + " Batch: processed " + recordsProcessed + " records out of "
            + totalRecords + " in " + elapsedTime + " seconds");
}

From source file:edu.scripps.fl.pipeline.StatelessSessionStage.java

License:Apache License

@Override
public void postprocess() throws StageException {
    try {/*ww w  .  j  a v a  2 s .c om*/
        for (Transaction tx : transactions.values())
            if (!tx.wasCommitted())
                tx.commit();
        for (StatelessSession session : sessions.values()) {
            session.close();
        }
        sessions = null;
        transactions = null;
    } catch (Exception ex) {
        throw new StageException(this, ex);
    }
}

From source file:edu.utah.further.core.data.hibernate.listeners.PreUpdatePreventNullOverwriteListener.java

License:Apache License

@SuppressWarnings("resource")
@Override/*from ww  w  . j a  v  a 2s. c om*/
public boolean onPreUpdate(final PreUpdateEvent event) {
    if (log.isDebugEnabled()) {
        log.debug("Received pre-update event");
    }
    final EntityPersister entityPersister = event.getPersister();
    final EntityMode entityMode = entityPersister.guessEntityMode(event.getEntity());
    final Connection connection = getConnection(entityPersister);
    final StatelessSession session = entityPersister.getFactory().openStatelessSession(connection);
    session.beginTransaction();
    final Serializable entityId = entityPersister.getIdentifier(event.getEntity(),
            (SessionImplementor) session);
    final Object existingEntity = session.get(event.getEntity().getClass(), entityId);

    if (log.isDebugEnabled()) {
        log.debug("Loaded existing entity " + existingEntity);
    }

    boolean updatedExistingEntity = false;
    for (int i = 0; i < entityPersister.getPropertyNames().length; i++) {
        final Object oldPropValue = entityPersister.getPropertyValue(existingEntity, i, entityMode);
        if (oldPropValue == null) {

            if (log.isDebugEnabled()) {
                log.debug("Found a property in the existing " + "entity that was null, checking new entity");
            }

            final Object newPropValue = entityPersister.getPropertyValue(event.getEntity(), i, entityMode);

            if (!(newPropValue instanceof Collection<?>) && newPropValue != null) {

                if (log.isDebugEnabled()) {
                    log.debug("The new entity contains a non-null " + "value, updating existing entity");
                }

                entityPersister.setPropertyValue(existingEntity, i, newPropValue, entityMode);
                updatedExistingEntity = true;
            }
        }
    }

    if (updatedExistingEntity) {
        entityPersister.getFactory().getCurrentSession().cancelQuery();
        session.update(existingEntity);
    }

    session.getTransaction().commit();
    session.close();

    return updatedExistingEntity;
}

From source file:eu.europa.ec.fisheries.uvms.spatial.service.dao.AbstractAreaDao.java

License:Open Source License

public List<Serializable> bulkInsert(Map<String, List<Property>> features, List<UploadMappingProperty> mapping)
        throws ServiceException {
    List<Serializable> invalidGeometryList = new ArrayList<>();
    StatelessSession session = (getEntityManager().unwrap(Session.class)).getSessionFactory()
            .openStatelessSession();/*from   w w w .  j a v a 2  s.c  o m*/
    Transaction tx = session.beginTransaction();
    try {
        Query query = session.getNamedQuery(getDisableAreaNamedQuery());
        query.executeUpdate();
        for (List<Property> properties : features.values()) {
            Map<String, Object> values = BaseAreaEntity.createAttributesMap(properties);
            BaseAreaEntity entity = createEntity(values, mapping);
            if (entity.getName() == null || entity.getCode() == null) {
                throw new ServiceException("NAME AND CODE FIELD ARE MANDATORY");
            }
            Serializable identifier = session.insert(entity);
            if (!entity.getGeom().isValid()) {
                invalidGeometryList.add(identifier);
            }
        }
        log.debug("Commit transaction");
        tx.commit();
    } catch (Exception e) {
        tx.rollback();
        throw new ServiceException("Rollback transaction", e);
    } finally {
        log.debug("Closing session");
        session.close();
    }
    return invalidGeometryList;
}

From source file:gov.nih.nci.cabig.caaers.dao.OrganizationDao.java

License:BSD License

public void saveAll(final List<Organization> organizations) {
    StatelessSession session = getHibernateTemplate().getSessionFactory().openStatelessSession();
    try {//from w w w.  ja  v  a2s  .co  m
        for (Organization o : organizations) {
            log.info("processing :" + o.getNciInstituteCode());
            if (o.getId() == null) {
                if (log.isInfoEnabled())
                    log.info("Inserting " + o.getNciInstituteCode());
                session.insert(o);
            } else {
                if (log.isInfoEnabled())
                    log.info("Updating " + o.getNciInstituteCode());
                session.update(o);
            }
        }
    } catch (HibernateException e) {
        throw getHibernateTemplate().convertHibernateAccessException(e);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        session.close();
    }
}

From source file:gr.abiss.calipso.service.impl.UserServiceImpl.java

License:Open Source License

@Override
@Transactional(readOnly = false)/*from w ww. jav a2 s .c o  m*/
public void expireResetPasswordTokens() {
    // get a hibernate session suitable for read-only access to large datasets
    StatelessSession session = ((Session) this.repository.getEntityManager().getDelegate()).getSessionFactory()
            .openStatelessSession();
    Date yesterday = DateUtils.addDays(new Date(), -1);

    // send email notifications for account confirmation tokens that expired
    org.hibernate.Query query = session.createQuery(
            "SELECT new gr.abiss.calipso.model.UserDTO(u.id, u.firstName, u.lastName,u.username, u.email, u.emailHash) FROM User u "
                    + "WHERE u.password IS NULL and u.resetPasswordTokenCreated IS NOT NULL and u.resetPasswordTokenCreated  < :yesterday");
    query.setParameter("yesterday", yesterday);
    query.setFetchSize(Integer.valueOf(1000));
    query.setReadOnly(true);
    query.setLockMode("a", LockMode.NONE);
    ScrollableResults results = query.scroll(ScrollMode.FORWARD_ONLY);
    while (results.next()) {
        UserDTO dto = (UserDTO) results.get(0);
        // TODO: send expiration email
        this.emailService.sendAccountConfirmationExpired(new User(dto));
    }
    results.close();
    session.close();

    // expire tokens, including password reset requests
    this.repository.expireResetPasswordTokens(yesterday);
}

From source file:kr.debop4j.data.hibernate.tools.StatelessTool.java

License:Apache License

/**
 *  session StatelessSession? ?  ? , .  @param session the session
 *
 * @param action the action/*from  w w  w  .  j a va  2 s. c om*/
 */
public static void executeTransactional(Session session, Action1<StatelessSession> action) {
    if (log.isDebugEnabled())
        log.debug("StatelessSession? ? Transaction ?  ? .");

    StatelessSession stateless = openStatelessSession(session);
    Transaction tx = null;

    try {
        tx = stateless.beginTransaction();
        action.perform(stateless);
        tx.commit();
    } catch (Exception e) {
        log.error("StatelessSession? ? ? . rollback .", e);
        if (tx != null)
            tx.rollback();
        throw new RuntimeException(e);
    } finally {
        stateless.close();
    }
}