/// <summary> /// Begin a new transaction with the given transaction definition. /// </summary> /// <param name="transaction"> /// Transaction object returned by /// <see cref="Spring.Transaction.Support.AbstractPlatformTransactionManager.DoGetTransaction"/>. /// </param> /// <param name="definition"> /// <see cref="Spring.Transaction.ITransactionDefinition"/> instance, describing /// propagation behavior, isolation level, timeout etc. /// </param> /// <remarks> /// Does not have to care about applying the propagation behavior, /// as this has already been handled by this abstract manager. /// </remarks> /// <exception cref="Spring.Transaction.TransactionException"> /// In the case of creation or system errors. /// </exception> protected override void DoBegin(object transaction, ITransactionDefinition definition) { DbProviderTransactionObject txMgrStateObject = (DbProviderTransactionObject)transaction; IDbConnection con = null; if (dbProvider == null) { throw new ArgumentException("DbProvider is required to be set on AdoPlatformTransactionManager"); } try { if (txMgrStateObject.ConnectionHolder == null || txMgrStateObject.ConnectionHolder.SynchronizedWithTransaction) { IDbConnection newCon = DbProvider.CreateConnection(); if (log.IsDebugEnabled) { log.Debug("Acquired Connection [" + newCon + ", " + newCon.ConnectionString + "] for ADO.NET transaction"); } newCon.Open(); //TODO isolation level mgmt - will need to abstract out SQL used to specify this in DbMetaData //MSDN docs... //With one exception, you can switch from one isolation level to another at any time during a transaction. The exception occurs when changing from any isolation level to SNAPSHOT isolation //IsolationLevel previousIsolationLevel = IDbTransaction newTrans = newCon.BeginTransaction(definition.TransactionIsolationLevel); txMgrStateObject.SetConnectionHolder(new ConnectionHolder(newCon, newTrans), true); } txMgrStateObject.ConnectionHolder.SynchronizedWithTransaction = true; con = txMgrStateObject.ConnectionHolder.Connection; txMgrStateObject.ConnectionHolder.TransactionActive = true; int timeout = DetermineTimeout(definition); if (timeout != DefaultTransactionDefinition.TIMEOUT_DEFAULT) { txMgrStateObject.ConnectionHolder.TimeoutInSeconds = timeout; } //Bind transactional resources to thread if (txMgrStateObject.NewConnectionHolder) { TransactionSynchronizationManager.BindResource(DbProvider, txMgrStateObject.ConnectionHolder); } } //TODO catch specific exception catch (Exception e) { ConnectionUtils.DisposeConnection(con, DbProvider); throw new CannotCreateTransactionException("Could not create ADO.NET connection for transaction", e); } }
/// <summary> /// Return the current transaction object. /// </summary> /// <returns>The current transaction object.</returns> /// <exception cref="Spring.Transaction.CannotCreateTransactionException"> /// If transaction support is not available. /// </exception> /// <exception cref="Spring.Transaction.TransactionException"> /// In the case of lookup or system errors. /// </exception> protected override object DoGetTransaction() { DbProviderTransactionObject txMgrStateObject = new DbProviderTransactionObject(); txMgrStateObject.SavepointAllowed = NestedTransactionsAllowed; ConnectionHolder conHolder = (ConnectionHolder)TransactionSynchronizationManager.GetResource(DbProvider); txMgrStateObject.SetConnectionHolder(conHolder, false); return(txMgrStateObject); }
/// <summary> /// Return the current transaction object. /// </summary> /// <returns>The current transaction object.</returns> /// <exception cref="Spring.Transaction.CannotCreateTransactionException"> /// If transaction support is not available. /// </exception> /// <exception cref="Spring.Transaction.TransactionException"> /// In the case of lookup or system errors. /// </exception> protected override object DoGetTransaction() { DbProviderTransactionObject txMgrStateObject = new DbProviderTransactionObject(); txMgrStateObject.SavepointAllowed = NestedTransactionsAllowed; ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.GetResource(DbProvider); txMgrStateObject.SetConnectionHolder(conHolder, false); return txMgrStateObject; }