Search in sources :

Example 11 with FlushMode

use of org.hibernate.FlushMode in project spring-framework by spring-projects.

the class HibernateJpaDialect method prepareTransaction.

@Override
public Object prepareTransaction(EntityManager entityManager, boolean readOnly, String name) throws PersistenceException {
    Session session = getSession(entityManager);
    FlushMode previousFlushMode = prepareFlushMode(session, readOnly);
    return new SessionTransactionData(session, previousFlushMode, null, null);
}
Also used : FlushMode(org.hibernate.FlushMode) Session(org.hibernate.Session)

Example 12 with FlushMode

use of org.hibernate.FlushMode in project spring-framework by spring-projects.

the class SpringSessionContext method currentSession.

/**
	 * Retrieve the Spring-managed Session for the current thread, if any.
	 */
@Override
@SuppressWarnings("deprecation")
public Session currentSession() throws HibernateException {
    Object value = TransactionSynchronizationManager.getResource(this.sessionFactory);
    if (value instanceof Session) {
        return (Session) value;
    } else if (value instanceof SessionHolder) {
        SessionHolder sessionHolder = (SessionHolder) value;
        Session session = sessionHolder.getSession();
        if (!sessionHolder.isSynchronizedWithTransaction() && TransactionSynchronizationManager.isSynchronizationActive()) {
            TransactionSynchronizationManager.registerSynchronization(new SpringSessionSynchronization(sessionHolder, this.sessionFactory, false));
            sessionHolder.setSynchronizedWithTransaction(true);
            // Switch to FlushMode.AUTO, as we have to assume a thread-bound Session
            // with FlushMode.MANUAL, which needs to allow flushing within the transaction.
            FlushMode flushMode = SessionFactoryUtils.getFlushMode(session);
            if (flushMode.equals(FlushMode.MANUAL) && !TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {
                session.setFlushMode(FlushMode.AUTO);
                sessionHolder.setPreviousFlushMode(flushMode);
            }
        }
        return session;
    }
    if (this.transactionManager != null) {
        try {
            if (this.transactionManager.getStatus() == Status.STATUS_ACTIVE) {
                Session session = this.jtaSessionContext.currentSession();
                if (TransactionSynchronizationManager.isSynchronizationActive()) {
                    TransactionSynchronizationManager.registerSynchronization(new SpringFlushSynchronization(session));
                }
                return session;
            }
        } catch (SystemException ex) {
            throw new HibernateException("JTA TransactionManager found but status check failed", ex);
        }
    }
    if (TransactionSynchronizationManager.isSynchronizationActive()) {
        Session session = this.sessionFactory.openSession();
        if (TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {
            session.setFlushMode(FlushMode.MANUAL);
        }
        SessionHolder sessionHolder = new SessionHolder(session);
        TransactionSynchronizationManager.registerSynchronization(new SpringSessionSynchronization(sessionHolder, this.sessionFactory, true));
        TransactionSynchronizationManager.bindResource(this.sessionFactory, sessionHolder);
        sessionHolder.setSynchronizedWithTransaction(true);
        return session;
    } else {
        throw new HibernateException("Could not obtain transaction-synchronized Session for current thread");
    }
}
Also used : SystemException(javax.transaction.SystemException) HibernateException(org.hibernate.HibernateException) FlushMode(org.hibernate.FlushMode) Session(org.hibernate.Session)

Example 13 with FlushMode

use of org.hibernate.FlushMode in project openmrs-core by openmrs.

the class HibernateAdministrationDAO method validate.

/**
 * @see org.openmrs.api.db.AdministrationDAO#validate(java.lang.Object, Errors)
 * @should Pass validation if field lengths are correct
 * @should Fail validation if field lengths are not correct
 * @should Fail validation for location class if field lengths are not correct
 * @should Pass validation for location class if field lengths are correct
 */
// @SuppressWarnings({ "deprecation", "unchecked", "rawtypes" })
@Override
public void validate(Object object, Errors errors) throws DAOException {
    Class entityClass = object.getClass();
    ClassMetadata metadata = sessionFactory.getClassMetadata(entityClass);
    if (metadata != null) {
        String[] propNames = metadata.getPropertyNames();
        Object identifierType = metadata.getIdentifierType();
        String identifierName = metadata.getIdentifierPropertyName();
        if (identifierType instanceof StringType || identifierType instanceof TextType) {
            int maxLength = getMaximumPropertyLength(entityClass, identifierName);
            String identifierValue = (String) metadata.getIdentifier(object, (SessionImplementor) sessionFactory.getCurrentSession());
            if (identifierValue != null) {
                int identifierLength = identifierValue.length();
                if (identifierLength > maxLength) {
                    errors.rejectValue(identifierName, "error.exceededMaxLengthOfField", new Object[] { maxLength }, null);
                }
            }
        }
        for (String propName : propNames) {
            Type propType = metadata.getPropertyType(propName);
            if (propType instanceof StringType || propType instanceof TextType) {
                String propertyValue = (String) metadata.getPropertyValue(object, propName);
                if (propertyValue != null) {
                    int maxLength = getMaximumPropertyLength(entityClass, propName);
                    int propertyValueLength = propertyValue.length();
                    if (propertyValueLength > maxLength) {
                        errors.rejectValue(propName, "error.exceededMaxLengthOfField", new Object[] { maxLength }, null);
                    }
                }
            }
        }
    }
    FlushMode previousFlushMode = sessionFactory.getCurrentSession().getFlushMode();
    sessionFactory.getCurrentSession().setFlushMode(FlushMode.MANUAL);
    try {
        for (Validator validator : getValidators(object)) {
            validator.validate(object, errors);
        }
    } finally {
        sessionFactory.getCurrentSession().setFlushMode(previousFlushMode);
    }
}
Also used : ClassMetadata(org.hibernate.metadata.ClassMetadata) TextType(org.hibernate.type.TextType) StringType(org.hibernate.type.StringType) Type(org.hibernate.type.Type) StringType(org.hibernate.type.StringType) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) PersistentClass(org.hibernate.mapping.PersistentClass) OpenmrsObject(org.openmrs.OpenmrsObject) FlushMode(org.hibernate.FlushMode) Validator(org.springframework.validation.Validator) TextType(org.hibernate.type.TextType)

Example 14 with FlushMode

use of org.hibernate.FlushMode in project openmrs-core by openmrs.

the class HibernateContextDAO method getUserByUuid.

/**
 * @see org.openmrs.api.db.ContextDAO#getUserByUuid(java.lang.String)
 */
@Override
@Transactional(readOnly = true)
public User getUserByUuid(String uuid) {
    // don't flush here in case we're in the AuditableInterceptor.  Will cause a StackOverflowEx otherwise
    FlushMode flushMode = sessionFactory.getCurrentSession().getFlushMode();
    sessionFactory.getCurrentSession().setFlushMode(FlushMode.MANUAL);
    User u = (User) sessionFactory.getCurrentSession().createQuery("from User u where u.uuid = :uuid").setString("uuid", uuid).uniqueResult();
    // reset the flush mode to whatever it was before
    sessionFactory.getCurrentSession().setFlushMode(flushMode);
    return u;
}
Also used : User(org.openmrs.User) FlushMode(org.hibernate.FlushMode) Transactional(org.springframework.transaction.annotation.Transactional)

Example 15 with FlushMode

use of org.hibernate.FlushMode in project openmrs-core by openmrs.

the class HibernateProgramWorkflowDAO method getPatientProgramByAttributeNameAndValue.

@Override
public List<PatientProgram> getPatientProgramByAttributeNameAndValue(String attributeName, String attributeValue) {
    FlushMode flushMode = sessionFactory.getCurrentSession().getFlushMode();
    sessionFactory.getCurrentSession().setFlushMode(FlushMode.MANUAL);
    Query query;
    try {
        query = sessionFactory.getCurrentSession().createQuery("SELECT pp FROM patient_program pp " + "INNER JOIN pp.attributes attr " + "INNER JOIN attr.attributeType attr_type " + "WHERE attr.valueReference = :attributeValue " + "AND attr_type.name = :attributeName " + "AND pp.voided = 0").setParameter("attributeName", attributeName).setParameter("attributeValue", attributeValue);
        return query.list();
    } finally {
        sessionFactory.getCurrentSession().setFlushMode(flushMode);
    }
}
Also used : Query(org.hibernate.Query) FlushMode(org.hibernate.FlushMode)

Aggregations

FlushMode (org.hibernate.FlushMode)19 Session (org.hibernate.Session)13 CartOperationRequest (org.broadleafcommerce.core.order.service.workflow.CartOperationRequest)4 Transactional (org.springframework.transaction.annotation.Transactional)4 UpdateCartException (org.broadleafcommerce.core.order.service.exception.UpdateCartException)3 WorkflowException (org.broadleafcommerce.core.workflow.WorkflowException)3 SQLQuery (org.hibernate.SQLQuery)3 Connection (java.sql.Connection)2 ArrayList (java.util.ArrayList)2 Order (org.broadleafcommerce.core.order.domain.Order)2 OrderItemRequestDTO (org.broadleafcommerce.core.order.service.call.OrderItemRequestDTO)2 AddToCartException (org.broadleafcommerce.core.order.service.exception.AddToCartException)2 RemoveFromCartException (org.broadleafcommerce.core.order.service.exception.RemoveFromCartException)2 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)2 Serializable (java.io.Serializable)1 Field (java.lang.reflect.Field)1 Date (java.util.Date)1 SystemException (javax.transaction.SystemException)1 AdminMainEntity (org.broadleafcommerce.common.admin.domain.AdminMainEntity)1 OfferAlreadyAddedException (org.broadleafcommerce.core.offer.service.exception.OfferAlreadyAddedException)1