Example usage for org.springframework.transaction.support DefaultTransactionDefinition setReadOnly

List of usage examples for org.springframework.transaction.support DefaultTransactionDefinition setReadOnly

Introduction

In this page you can find the example usage for org.springframework.transaction.support DefaultTransactionDefinition setReadOnly.

Prototype

public final void setReadOnly(boolean readOnly) 

Source Link

Document

Set whether to optimize as read-only transaction.

Usage

From source file:org.eurekastreams.server.service.servlets.UploadBannerServlet.java

/**
 * Gets the domain entity.//w  w w .ja  va  2s  .c o m
 * 
 * @param inName
 *            the name used to find the domain entity
 * @param request
 *            the http request object.
 * @return the domain entity
 * @throws ServletException
 *             when anything goes wrong
 */
@SuppressWarnings("deprecation")
@Override
protected DomainEntity getDomainEntity(final String inName, final HttpServletRequest request)
        throws ServletException {
    PlatformTransactionManager transMgr = (PlatformTransactionManager) getSpringContext()
            .getBean("transactionManager");
    DefaultTransactionDefinition transDef = new DefaultTransactionDefinition();
    transDef.setName("getOrg");
    transDef.setReadOnly(true);
    TransactionStatus transStatus = transMgr.getTransaction(transDef);

    String type = request.getParameter("type");

    GetAllPersonIdsWhoHaveGroupCoordinatorAccess groupCoordinatorMapper = //
            (GetAllPersonIdsWhoHaveGroupCoordinatorAccess) getSpringContext()
                    .getBean("getAllPersonIdsWhoHaveGroupCoordinatorAccess");

    DomainGroupMapper groupMapper = (DomainGroupMapper) getSpringContext().getBean("jpaGroupMapper");

    String entityName = request.getParameter("entityName");

    if (type.equals("DomainGroup")) {
        DomainGroup group = groupMapper.findByShortName(entityName);

        if (group == null) {
            throw new ServletException("Group:  " + entityName + " not found");
        }

        if (groupCoordinatorMapper.hasGroupCoordinatorAccessRecursively(inName, group.getId())) {
            transMgr.commit(transStatus);
            return group;
        }

        throw new ServletException("User " + inName + " is not a coordinator of group:  " + group.getName());
    }

    else {

        throw new ServletException("Type of object is " + type + " is not supported");
    }

}

From source file:org.opendatakit.common.persistence.engine.pgres.DatastoreImpl.java

/**
 * Relation manipulation APIs//from   ww  w .j a  va2s  .c  o m
 */
@Override
public void assertRelation(CommonFieldsBase relation, User user) throws ODKDatastoreException {
    JdbcTemplate jc = getJdbcConnection();
    TransactionStatus status = null;
    try {
        DefaultTransactionDefinition paramTransactionDefinition = new DefaultTransactionDefinition();

        // do serializable read on the information schema...
        paramTransactionDefinition.setIsolationLevel(DefaultTransactionDefinition.ISOLATION_SERIALIZABLE);
        paramTransactionDefinition.setReadOnly(true);
        status = tm.getTransaction(paramTransactionDefinition);

        // see if relation already is defined and update it with dimensions...
        if (updateRelation(jc, relation, null)) {
            // it exists -- we're done!
            tm.commit(status);
            status = null;
            return;
        } else {
            tm.commit(status);
            // Try a new transaction to create the table
            paramTransactionDefinition.setIsolationLevel(DefaultTransactionDefinition.ISOLATION_SERIALIZABLE);
            paramTransactionDefinition.setReadOnly(false);
            status = tm.getTransaction(paramTransactionDefinition);

            // need to create the table...
            StringBuilder b = new StringBuilder();
            b.append(K_CREATE_TABLE);
            b.append(K_BQ);
            b.append(relation.getSchemaName());
            b.append(K_BQ);
            b.append(".");
            b.append(K_BQ);
            b.append(relation.getTableName());
            b.append(K_BQ);
            b.append(K_OPEN_PAREN);
            boolean firstTime = true;
            for (DataField f : relation.getFieldList()) {
                if (!firstTime) {
                    b.append(K_CS);
                }
                firstTime = false;
                b.append(K_BQ);
                b.append(f.getName());
                b.append(K_BQ);
                DataField.DataType type = f.getDataType();
                switch (type) {
                case BINARY:
                    b.append(" BYTEA");
                    break;
                case LONG_STRING:
                    b.append(" TEXT");// b.append(" CHARACTER SET utf8");
                    break;
                case STRING:
                    b.append(" VARCHAR(");
                    Long len = f.getMaxCharLen();
                    if (len == null) {
                        len = PersistConsts.DEFAULT_MAX_STRING_LENGTH;
                    }
                    b.append(len.toString());
                    b.append(K_CLOSE_PAREN);
                    // b.append(" CHARACTER SET utf8");
                    break;
                case BOOLEAN:
                    b.append(" BOOLEAN");
                    break;
                case INTEGER:
                    Integer int_digits = f.getNumericPrecision();
                    if (int_digits == null) {
                        int_digits = DEFAULT_INT_NUMERIC_PRECISION;
                    }
                    Integer isSerial = f.getNumericScale();
                    // logger.warn("********************");
                    // logger.warn(int_digits + " ---" + isSerial);
                    if (int_digits.compareTo(9) > 0) {
                        if (isSerial != null && isSerial == 1) {
                            b.append(" SERIAL");
                            continue;
                            //    logger.warn("^^^^^^^^^^^^^^^^");
                        } else {
                            b.append(" BIGINT");
                        }
                    } else {
                        b.append(" INTEGER");
                    }
                    break;
                case DECIMAL:
                    Integer dbl_digits = f.getNumericPrecision();
                    Integer dbl_fract = f.getNumericScale();
                    if (dbl_digits == null) {
                        dbl_digits = DEFAULT_DBL_NUMERIC_PRECISION;
                    }
                    if (dbl_fract == null) {
                        dbl_fract = DEFAULT_DBL_NUMERIC_SCALE;
                    }
                    b.append(" DECIMAL(");
                    b.append(dbl_digits.toString());
                    b.append(K_CS);
                    b.append(dbl_fract.toString());
                    b.append(K_CLOSE_PAREN);
                    break;
                case DATETIME:
                    b.append(" TIMESTAMP WITHOUT TIME ZONE");
                    break;
                case URI:
                    b.append(" VARCHAR(");
                    len = f.getMaxCharLen();
                    if (len == null) {
                        len = PersistConsts.URI_STRING_LEN;
                    }
                    b.append(len.toString());
                    b.append(")");// b.append(" CHARACTER SET utf8");
                    break;
                }

                if (f == relation.primaryKey) {
                    b.append(" UNIQUE ");
                }
                if (f.getNullable()) {
                    b.append(" NULL ");
                } else {
                    b.append(" NOT NULL ");
                }
            }
            b.append(K_CLOSE_PAREN);

            String createTableStmt = b.toString();
            LogFactory.getLog(DatastoreImpl.class).info("Attempting: " + createTableStmt);

            jc.execute(createTableStmt);
            LogFactory.getLog(DatastoreImpl.class)
                    .info("create table success (before updateRelation): " + relation.getTableName());

            String idx;
            // create other indicies
            for (DataField f : relation.getFieldList()) {
                if ((f.getIndexable() != IndexType.NONE) && (f != relation.primaryKey)) {
                    idx = relation.getTableName() + "_" + shortPrefix(f.getName());
                    createIndex(jc, relation, idx, f);
                }
            }

            // and update the relation with actual dimensions...
            updateRelation(jc, relation, createTableStmt);
            tm.commit(status);
        }
    } catch (Exception e) {
        if (status != null) {
            tm.rollback(status);
        }
        throw new ODKDatastoreException(e);
    }
}

From source file:org.opendatakit.common.persistence.engine.sqlserver.DatastoreImpl.java

/**
 * Relation manipulation APIs//from   w  ww.  j  av a 2  s  .c  om
 */
@Override
public void assertRelation(CommonFieldsBase relation, User user) throws ODKDatastoreException {
    JdbcTemplate jc = getJdbcConnection();
    TransactionStatus status = null;
    try {
        DefaultTransactionDefinition paramTransactionDefinition = new DefaultTransactionDefinition();

        // do serializable read on the information schema...
        paramTransactionDefinition.setIsolationLevel(DefaultTransactionDefinition.ISOLATION_SERIALIZABLE);
        paramTransactionDefinition.setReadOnly(true);
        status = tm.getTransaction(paramTransactionDefinition);

        // see if relation already is defined and update it with dimensions...
        if (updateRelation(jc, relation, null)) {
            // it exists -- we're done!
            tm.commit(status);
            status = null;
            return;
        } else {
            tm.commit(status);
            // Try a new transaction to create the table
            paramTransactionDefinition.setIsolationLevel(DefaultTransactionDefinition.ISOLATION_SERIALIZABLE);
            paramTransactionDefinition.setReadOnly(false);
            status = tm.getTransaction(paramTransactionDefinition);

            // total number of columns must be less than MAX_BIND_PARAMS
            int countColumns = 0;
            // need to create the table...
            StringBuilder b = new StringBuilder();
            b.append(K_CREATE_TABLE);
            b.append(K_BQ);
            b.append(relation.getSchemaName());
            b.append(K_BQ);
            b.append(".");
            b.append(K_BQ);
            b.append(relation.getTableName());
            b.append(K_BQ);
            b.append(K_OPEN_PAREN);
            boolean firstTime = true;
            for (DataField f : relation.getFieldList()) {
                if (!firstTime) {
                    b.append(K_CS);
                }
                ++countColumns;
                firstTime = false;
                String nullClause;
                if (f.getNullable()) {
                    nullClause = K_NULL;
                } else {
                    nullClause = K_NOT_NULL;
                }

                b.append(K_BQ);
                b.append(f.getName());
                b.append(K_BQ);
                DataField.DataType type = f.getDataType();
                switch (type) {
                case BINARY:
                    b.append(" varbinary(max)").append(nullClause);
                    break;
                case LONG_STRING:
                    b.append(" nvarchar(max)").append(nullClause);
                    break;
                case STRING:
                    b.append(" nvarchar(");
                    Long len = f.getMaxCharLen();
                    if (len == null) {
                        len = PersistConsts.DEFAULT_MAX_STRING_LENGTH;
                    }
                    if (len > MAX_IN_ROW_NVARCHAR) {
                        // store value out-of-row
                        b.append("max");
                    } else {
                        b.append(len.toString());
                    }
                    b.append(K_CLOSE_PAREN).append(nullClause);
                    break;
                case BOOLEAN:
                    b.append(" bit").append(nullClause);
                    break;
                case INTEGER:
                    Integer int_digits = f.getNumericPrecision();
                    if (int_digits == null) {
                        int_digits = DEFAULT_INT_NUMERIC_PRECISION;
                    }

                    if (int_digits.compareTo(9) > 0) {
                        b.append(" bigint").append(nullClause);
                    } else {
                        b.append(" integer").append(nullClause);
                    }
                    break;
                case DECIMAL:
                    if (f == relation.primaryKey) {
                        throw new IllegalStateException("cannot use decimal columns as primary keys");
                    }

                    if (f.isDoublePrecision()) {
                        b.append(" float(53) ").append(nullClause);
                    } else {
                        Integer dbl_digits = f.getNumericPrecision();
                        Integer dbl_fract = f.getNumericScale();
                        if (dbl_digits == null) {
                            dbl_digits = DEFAULT_DBL_NUMERIC_PRECISION;
                        }
                        if (dbl_fract == null) {
                            dbl_fract = DEFAULT_DBL_NUMERIC_SCALE;
                        }
                        b.append(" decimal(");
                        b.append(dbl_digits.toString());
                        b.append(K_CS);
                        b.append(dbl_fract.toString());
                        b.append(K_CLOSE_PAREN).append(nullClause);
                    }
                    break;
                case DATETIME:
                    b.append(" datetime2(7)").append(nullClause);
                    break;
                case URI:
                    b.append(" nvarchar(");
                    len = f.getMaxCharLen();
                    if (len == null) {
                        len = PersistConsts.URI_STRING_LEN;
                    }
                    b.append(len.toString());
                    b.append(")").append(nullClause);
                    break;
                }

                if (f == relation.primaryKey) {
                    b.append(" PRIMARY KEY NONCLUSTERED ");
                }
            }
            b.append(K_CLOSE_PAREN);

            if (countColumns > MAX_BIND_PARAMS) {
                throw new IllegalArgumentException("Table size exceeds bind parameter limit");
            }

            String createTableStmt = b.toString();
            LogFactory.getLog(DatastoreImpl.class).info("Attempting: " + createTableStmt);

            jc.execute(createTableStmt);
            LogFactory.getLog(DatastoreImpl.class)
                    .info("create table success (before updateRelation): " + relation.getTableName());

            boolean alreadyClustered = false;
            String idx;
            // create other indicies
            for (DataField f : relation.getFieldList()) {
                if ((f.getIndexable() != IndexType.NONE) && (f != relation.primaryKey)) {
                    idx = relation.getTableName() + "_" + shortPrefix(f.getName());
                    alreadyClustered = createIndex(jc, relation, idx, f, alreadyClustered);
                }
            }

            // and update the relation with actual dimensions...
            updateRelation(jc, relation, createTableStmt);
            tm.commit(status);
        }
    } catch (Exception e) {
        if (status != null) {
            tm.rollback(status);
        }
        throw new ODKDatastoreException(e);
    }
}