List of usage examples for org.springframework.transaction TransactionDefinition isReadOnly
default boolean isReadOnly()
From source file:com._4dconcept.springframework.data.marklogic.datasource.ContentSourceUtils.java
/** * Prepare the given Session with the given transaction semantics. * @param ses the Session to prepare//w ww .j av a 2 s . com * @param definition the transaction definition to apply * @return the previous isolation level, if any * @throws XccException if thrown by XDBC methods * @see #resetSessionAfterTransaction */ public static Integer prepareSessionForTransaction(Session ses, TransactionDefinition definition) throws XccException { Assert.notNull(ses, "No Session specified"); if (definition != null && definition.isReadOnly()) { if (logger.isDebugEnabled()) { logger.debug("Setting XDBC Session [" + ses + "] read-only"); } ses.setTransactionMode(Session.TransactionMode.QUERY); } else { ses.setTransactionMode(Session.TransactionMode.UPDATE); } // Apply specific isolation level, if any. Integer previousIsolationLevel = null; if (definition != null && definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) { if (logger.isDebugEnabled()) { logger.debug("Changing isolation level of XDBC Session [" + ses + "] to " + definition.getIsolationLevel()); } int currentIsolation = ses.getTransactionMode().ordinal(); if (currentIsolation != definition.getIsolationLevel()) { previousIsolationLevel = currentIsolation; ses.setTransactionMode(Session.TransactionMode.values()[definition.getIsolationLevel()]); } } return previousIsolationLevel; }
From source file:org.cfr.capsicum.datasource.DataSourceUtils.java
/** * Prepare the given Connection with the given transaction semantics. * @param con the Connection to prepare/*from w w w. j a v a 2 s. c o m*/ * @param definition the transaction definition to apply * @return the previous isolation level, if any * @throws SQLException if thrown by JDBC methods * @see #resetConnectionAfterTransaction */ public static Integer prepareConnectionForTransaction(Connection con, TransactionDefinition definition) throws SQLException { Assert.notNull(con, "No Connection specified"); // Set read-only flag. if (definition != null && definition.isReadOnly()) { try { if (logger.isDebugEnabled()) { logger.debug("Setting JDBC Connection [" + con + "] read-only"); } con.setReadOnly(true); } catch (Throwable ex) { // SQLException or UnsupportedOperationException // -> ignore, it's just a hint anyway. logger.debug("Could not set JDBC Connection read-only", ex); } } // Apply specific isolation level, if any. Integer previousIsolationLevel = null; if (definition != null && definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) { if (logger.isDebugEnabled()) { logger.debug("Changing isolation level of JDBC Connection [" + con + "] to " + definition.getIsolationLevel()); } previousIsolationLevel = new Integer(con.getTransactionIsolation()); con.setTransactionIsolation(definition.getIsolationLevel()); } return previousIsolationLevel; }
From source file:eu.trentorise.smartcampus.permissionprovider.oauth.IsolationSupportHibernateJpaDialect.java
@Override public Object beginTransaction(EntityManager entityManager, TransactionDefinition definition) throws PersistenceException, SQLException, TransactionException { boolean readOnly = definition.isReadOnly(); Connection connection = this.getJdbcConnection(entityManager, readOnly).getConnection(); connectionThreadLocal.set(connection); originalIsolation.set(DataSourceUtils.prepareConnectionForTransaction(connection, definition)); entityManager.getTransaction().begin(); return prepareTransaction(entityManager, readOnly, definition.getName()); }
From source file:org.topazproject.otm.spring.OtmTransactionManager.java
@Override public void doBegin(Object transaction, TransactionDefinition definition) throws TransactionException { try {/*from w w w . jav a2 s.c o m*/ Session s = ((TransactionObject) transaction).getSession(); s.beginTransaction(definition.isReadOnly(), determineTimeout(definition)); if (definition.isReadOnly() && skipFlushForRoTx) { ((TransactionObject) transaction).savedFlushMode = s.getFlushMode(); s.setFlushMode(Session.FlushMode.commit); } } catch (OtmException oe) { throw new TransactionSystemException("error beginning transaction", oe); } }
From source file:org.grails.datastore.mapping.transactions.DatastoreTransactionManager.java
@Override protected void doBegin(Object o, TransactionDefinition definition) throws TransactionException { TransactionObject txObject = (TransactionObject) o; Session session = null;/*from w w w .ja va 2 s . c om*/ try { session = txObject.getSessionHolder().getSession(); if (definition.isReadOnly()) { // Just set to NEVER in case of a new Session for this transaction. session.setFlushMode(FlushModeType.COMMIT); } Transaction<?> tx; // Register transaction timeout. int timeout = determineTimeout(definition); if (timeout != TransactionDefinition.TIMEOUT_DEFAULT) { tx = session.beginTransaction(); tx.setTimeout(timeout); } else { // Open a plain Datastore transaction without specified timeout. tx = session.beginTransaction(); } // Add the Datastore transaction to the session holder. txObject.setTransaction(tx); // Bind the session holder to the thread. if (txObject.isNewSessionHolder()) { TransactionSynchronizationManager.bindResource(getDatastore(), txObject.getSessionHolder()); } txObject.getSessionHolder().setSynchronizedWithTransaction(true); } catch (Exception ex) { if (txObject.isNewSession()) { try { if (session != null && session.getTransaction().isActive()) { session.getTransaction().rollback(); } } catch (Throwable ex2) { logger.debug("Could not rollback Session after failed transaction begin", ex); } finally { DatastoreUtils.closeSession(session); } } throw new CannotCreateTransactionException("Could not open Datastore Session for transaction", ex); } }
From source file:pl.com.bottega.testutils.HibernateExtendedJpaDialect.java
/** * This method is overridden to set custom isolation levels on the connection * @param entityManager/* ww w . j ava 2 s . c o m*/ * @param definition * @return * @throws PersistenceException * @throws SQLException * @throws TransactionException */ @Override public Object beginTransaction(final EntityManager entityManager, final TransactionDefinition definition) throws PersistenceException, SQLException, TransactionException { Session session = (Session) entityManager.getDelegate(); if (definition.getTimeout() != TransactionDefinition.TIMEOUT_DEFAULT) { getSession(entityManager).getTransaction().setTimeout(definition.getTimeout()); } entityManager.getTransaction().begin(); logger.debug("Transaction started"); session.doWork(new Work() { public void execute(Connection connection) throws SQLException { logger.debug("The connection instance is {}", connection); logger.debug( "The isolation level of the connection is {} and the isolation level set on the transaction is {}", connection.getTransactionIsolation(), definition.getIsolationLevel()); DataSourceUtils.prepareConnectionForTransaction(connection, definition); } }); return prepareTransaction(entityManager, definition.isReadOnly(), definition.getName()); }
From source file:com.newmainsoftech.spray.slingong.datastore.Slim3PlatformTransactionManager.java
@Override protected DefaultTransactionStatus newTransactionStatus(TransactionDefinition definition, Object transaction, boolean newTransaction, boolean newSynchronization, boolean debug, Object suspendedResources) { // Validate isolation level and time-out settings validateIsolationLevel(definition.getIsolationLevel()); boolean actualNewSynchronization = newSynchronization && !TransactionSynchronizationManager.isSynchronizationActive(); GlobalTransaction gtx = (GlobalTransaction) transaction; Set<GlobalTransactionState> gtxStateSet = getGlobalTransactionStates(gtx); GlobalTransaction newGtx;//from w ww. j a va2s . c om switch (definition.getPropagationBehavior()) { case TransactionAttribute.PROPAGATION_NESTED: if (gtxStateSet.contains(GlobalTransactionState.GlobalTransactionInstance)) { if (!isNestedTransactionAllowed()) { throw new TransactionSystemException(String.format( "Unexpected system state: the value of nestedTransactionAllowed boolean " + "member field is false. It should have been checked early as true within " + "handleExistingTransaction method, in order to provide the nested " + "propagation behavior over the transaction (what has already begun " + "GlobalTransaction instance (ID:%1$s)) by nested begin and commit/rollback calls.", (gtxStateSet.contains(GlobalTransactionState.GlobalTransactionInstance) ? gtx.getId() : "null"))); } if (logger.isInfoEnabled()) { logger.info(String.format( "Going to provide nested propagation behavior by nested begin and " + "commit/rollback calls on new GlobalTransaction instance over the " + "transaction what has already begun GlobalTransaction instance (ID:%1$s).", gtx.getId())); } } // Enter to below logic (for TransactionAttribute.PROPAGATION_REQUIRES_NEW case) case TransactionAttribute.PROPAGATION_REQUIRES_NEW: // Sanity check on system state if (!newTransaction) { throw new IllegalTransactionStateException(String.format( "Unexpected system state: the value of newTransaction boolean member field " + "is false for %1$s propagation behavior (%2$d). Exptected it to be true.", getPropagationBehaviorStr(definition.getPropagationBehavior()), definition.getPropagationBehavior())); } // Sanity check on current GlobalTransaction instances if (gtxStateSet.contains(GlobalTransactionState.GlobalTransactionInstance)) { validateCurrentActiveGlobalTransactionToParticipate(gtx, definition); } // begin new GlobalTransaction newGtx = beginGlogalTransaction(gtx, gtxStateSet, definition); return new DefaultTransactionStatus(newGtx, newTransaction, actualNewSynchronization, definition.isReadOnly(), debug, suspendedResources); case TransactionAttribute.PROPAGATION_REQUIRED: if (newTransaction) { // Sanity check on current GlobalTransaction instances if (gtxStateSet.contains(GlobalTransactionState.GlobalTransactionInstance)) { throw new IllegalTransactionStateException(String.format( "Unexpected system state: the value of newTransaction boolean member " + "field is true what direct to create new transaction for %1$s " + "propagation behavior (%2$d) though this transaction has already begun " + "the GlobalTransaction instance (ID:%3$s). Exptected it to be false and " + "participate to the existing transaction.", getPropagationBehaviorStr(definition.getPropagationBehavior()), definition.getPropagationBehavior(), gtx.getId())); } // begin new GlobalTransaction newGtx = beginGlogalTransaction(gtx, gtxStateSet, definition); return new DefaultTransactionStatus(newGtx, newTransaction, actualNewSynchronization, definition.isReadOnly(), debug, suspendedResources); } else { // Sanity check on current GlobalTransaction instances validateCurrentActiveGlobalTransactionToParticipate(gtx, definition); return new DefaultTransactionStatus(gtx, newTransaction, actualNewSynchronization, definition.isReadOnly(), debug, suspendedResources); } case TransactionAttribute.PROPAGATION_NEVER: if (newTransaction && !gtxStateSet.contains(GlobalTransactionState.GlobalTransactionInstance)) { return new DefaultTransactionStatus(null, newTransaction, actualNewSynchronization, definition.isReadOnly(), debug, suspendedResources); } else { throw new IllegalTransactionStateException(String.format( "Unexpected system state: for %1$s propagation behavior (%2$d), the value of " + "newTransaction boolean member field is expected to be true (actual: %3$b) " + "and transaction Object argument is expected to hold current active " + "GlobalTransaction instance (actual: %4$s %5$s one (ID:%6$s)).", getPropagationBehaviorStr(definition.getPropagationBehavior()), definition.getPropagationBehavior(), newTransaction, (gtxStateSet.contains(GlobalTransactionState.CurrentGlobalTransaction) ? "current" : "not-current"), (gtxStateSet.contains(GlobalTransactionState.ActiveGlobalTransaction) ? "active" : "inactive"), (gtxStateSet.contains(GlobalTransactionState.GlobalTransactionInstance) ? gtx.getId() : "null"))); } case TransactionAttribute.PROPAGATION_NOT_SUPPORTED: if (gtxStateSet.contains(GlobalTransactionState.GlobalTransactionInstance)) { throw new IllegalTransactionStateException(String.format( "Unexpected system state: for %1$s propagation behavior (%2$d), the " + "transaction Object argument is expected to hold null value (actual: " + "%3$s %4$s GlobalTransaction instance (ID:%5$s)).", getPropagationBehaviorStr(definition.getPropagationBehavior()), definition.getPropagationBehavior(), (gtxStateSet.contains(GlobalTransactionState.CurrentGlobalTransaction) ? "current" : "not-current"), (gtxStateSet.contains(GlobalTransactionState.ActiveGlobalTransaction) ? "active" : "inactive"), gtx.getId())); } // Create DeafultTransactionState with null transaction return new DefaultTransactionStatus(null, newTransaction, actualNewSynchronization, definition.isReadOnly(), debug, suspendedResources); case TransactionAttribute.PROPAGATION_SUPPORTS: if (newTransaction) { if (gtxStateSet.contains(GlobalTransactionState.GlobalTransactionInstance)) { throw new IllegalTransactionStateException(String.format( "Unexpected system state: for %1$s propagation behavior (%2$d), when the " + "newTransaction is true, the transaction Object argument is expected to " + "hold null value (actual: %3$s %4$s GlobalTransaction instance (ID:%5$s)).", getPropagationBehaviorStr(definition.getPropagationBehavior()), definition.getPropagationBehavior(), (gtxStateSet.contains(GlobalTransactionState.CurrentGlobalTransaction) ? "current" : "not-current"), (gtxStateSet.contains(GlobalTransactionState.ActiveGlobalTransaction) ? "active" : "inactive"), gtx.getId())); } return new DefaultTransactionStatus(null, newTransaction, actualNewSynchronization, definition.isReadOnly(), debug, suspendedResources); } else { if (!gtxStateSet.contains(GlobalTransactionState.ActiveGlobalTransaction) || !gtxStateSet.contains(GlobalTransactionState.CurrentGlobalTransaction)) { throw new IllegalTransactionStateException(String.format( "Unexpected system state: for %1$s propagation behavior (%2$d), when the " + "newTransaction is false, the transaction Object argument is expected to " + "hold current active GlobalTransaction instance (actual: %3$s %4$s " + "GlobalTransaction instance (ID:%5$s)).", getPropagationBehaviorStr(definition.getPropagationBehavior()), definition.getPropagationBehavior(), (gtxStateSet.contains(GlobalTransactionState.CurrentGlobalTransaction) ? "current" : "not-current"), (gtxStateSet.contains(GlobalTransactionState.ActiveGlobalTransaction) ? "active" : "inactive"), (gtxStateSet.contains(GlobalTransactionState.GlobalTransactionInstance) ? gtx.getId() : "null"))); } return new DefaultTransactionStatus(gtx, newTransaction, actualNewSynchronization, definition.isReadOnly(), debug, suspendedResources); } case TransactionAttribute.PROPAGATION_MANDATORY: if (newTransaction || !gtxStateSet.contains(GlobalTransactionState.ActiveGlobalTransaction) || !gtxStateSet.contains(GlobalTransactionState.CurrentGlobalTransaction)) { throw new IllegalTransactionStateException(String.format( "Unexpected system state: for %1$s propagation behavior (%2$d), the " + "newTransaction is expected to be false (actual: %3$b) and the transaction " + "Object argument is expected to hold current active GlobalTransaction " + "instance (actual: %3$s %4$s GlobalTransaction instance (ID:%5$s)).", getPropagationBehaviorStr(definition.getPropagationBehavior()), definition.getPropagationBehavior(), (gtxStateSet.contains(GlobalTransactionState.CurrentGlobalTransaction) ? "current" : "not-current"), (gtxStateSet.contains(GlobalTransactionState.ActiveGlobalTransaction) ? "active" : "inactive"), (gtxStateSet.contains(GlobalTransactionState.GlobalTransactionInstance) ? gtx.getId() : "null"))); } return new DefaultTransactionStatus(gtx, newTransaction, actualNewSynchronization, definition.isReadOnly(), debug, suspendedResources); default: throw new IllegalTransactionStateException(String.format( "Unknown propagation behavior (%1$d) is specified. Supported propagation " + "behaviors is either PROPAGATION_MANDATORY (%2$d), PROPAGATION_NESTED (%3$d), " + "PROPAGATION_NEVER (%4$d), PROPAGATION_NOT_SUPPORTED (%5$d), " + "PROPAGATION_REQUIRED (%6$d), PROPAGATION_REQUIRES_NEW (%7$d), " + "or PROPAGATION_SUPPORTS (%8$d).", definition.getPropagationBehavior(), TransactionAttribute.PROPAGATION_MANDATORY, TransactionAttribute.PROPAGATION_NESTED, TransactionAttribute.PROPAGATION_NEVER, TransactionAttribute.PROPAGATION_NOT_SUPPORTED, TransactionAttribute.PROPAGATION_REQUIRED, TransactionAttribute.PROPAGATION_REQUIRES_NEW, TransactionAttribute.PROPAGATION_SUPPORTS)); } // switch }
From source file:jp.terasoluna.fw.batch.util.BatchUtil.java
/** * ??/*from w w w .ja v a 2s .c o m*/ * @param tran PlatformTransactionManager * @param def TransactionDefinition * @param log Log * @return TransactionStatus */ public static TransactionStatus startTransaction(PlatformTransactionManager tran, TransactionDefinition def, Log log) { if (log != null && log.isDebugEnabled()) { logDebug(log, LogId.DAL025033, tran); if (def != null) { logDebug(log, LogId.DAL025034, def.getPropagationBehavior(), def.getIsolationLevel(), def.getTimeout(), def.isReadOnly(), def.getName()); } } TransactionStatus stat = null; if (tran != null) { stat = tran.getTransaction(def); } if (log != null && log.isDebugEnabled()) { logDebug(log, LogId.DAL025035, stat); } return stat; }
From source file:jp.terasoluna.fw.batch.util.BatchUtil.java
/** * ??/*from w ww .j a va2 s . c o m*/ * @param tranDef TransactionDefinition * @param tranMap PlatformTransactionManager * @param log Log * @return TransactionStatus */ public static Map<String, TransactionStatus> startTransactions(TransactionDefinition tranDef, Map<?, ?> tranMap, Log log) { Map<String, TransactionStatus> statMap = new LinkedHashMap<String, TransactionStatus>(); if (!tranMap.isEmpty()) { for (Map.Entry<?, ?> ent : tranMap.entrySet()) { String key = null; PlatformTransactionManager ptm = null; // ?? if (ent.getKey() instanceof String) { key = (String) ent.getKey(); } // ??? if (ent.getValue() instanceof PlatformTransactionManager) { ptm = (PlatformTransactionManager) ent.getValue(); } if (ptm != null) { if (log != null && log.isDebugEnabled()) { logDebug(log, LogId.DAL025033, key); if (tranDef != null) { logDebug(log, LogId.DAL025034, tranDef.getPropagationBehavior(), tranDef.getIsolationLevel(), tranDef.getTimeout(), tranDef.isReadOnly(), tranDef.getName()); } } // TransactionStatus trnStat = null; try { trnStat = ptm.getTransaction(tranDef); } catch (TransactionException e) { if (log != null && log.isErrorEnabled()) { logError(log, LogId.EAL025048, e, key); } endTransactions(tranMap, statMap, log); throw e; } // ? if (statMap != null) { statMap.put(key, trnStat); } if (log != null && log.isDebugEnabled()) { logDebug(log, LogId.DAL025035, key, trnStat); } } } } return statMap; }
From source file:org.springframework.jdbc.datasource.DataSourceUtils.java
/** * Prepare the given Connection with the given transaction semantics. * @param con the Connection to prepare/*w w w .java2 s.co m*/ * @param definition the transaction definition to apply * @return the previous isolation level, if any * @throws SQLException if thrown by JDBC methods * @see #resetConnectionAfterTransaction */ @Nullable public static Integer prepareConnectionForTransaction(Connection con, @Nullable TransactionDefinition definition) throws SQLException { Assert.notNull(con, "No Connection specified"); // Set read-only flag. if (definition != null && definition.isReadOnly()) { try { if (logger.isDebugEnabled()) { logger.debug("Setting JDBC Connection [" + con + "] read-only"); } con.setReadOnly(true); } catch (SQLException | RuntimeException ex) { Throwable exToCheck = ex; while (exToCheck != null) { if (exToCheck.getClass().getSimpleName().contains("Timeout")) { // Assume it's a connection timeout that would otherwise get lost: e.g. from JDBC 4.0 throw ex; } exToCheck = exToCheck.getCause(); } // "read-only not supported" SQLException -> ignore, it's just a hint anyway logger.debug("Could not set JDBC Connection read-only", ex); } } // Apply specific isolation level, if any. Integer previousIsolationLevel = null; if (definition != null && definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) { if (logger.isDebugEnabled()) { logger.debug("Changing isolation level of JDBC Connection [" + con + "] to " + definition.getIsolationLevel()); } int currentIsolation = con.getTransactionIsolation(); if (currentIsolation != definition.getIsolationLevel()) { previousIsolationLevel = currentIsolation; con.setTransactionIsolation(definition.getIsolationLevel()); } } return previousIsolationLevel; }