protected virtual void CreateTemporaryTableIfNecessary(IQueryable persister, ISessionImplementor session)
        {
            // Don't really know all the codes required to adequately decipher returned ADO exceptions here.
            // simply allow the failure to be eaten and the subsequent insert-selects/deletes should fail
            IIsolatedWork work = new TmpIdTableCreationIsolatedWork(persister, log, session);

            if (ShouldIsolateTemporaryTableDDL())
            {
                if (Factory.Settings.IsDataDefinitionInTransactionSupported)
                {
                    Isolater.DoIsolatedWork(work, session);
                }
                else
                {
                    Isolater.DoNonTransactedWork(work, session);
                }
            }
            else
            {
                using (var dummyCommand = session.ConnectionManager.CreateCommand())
                {
                    work.DoWork(dummyCommand.Connection, dummyCommand.Transaction);
                    session.ConnectionManager.AfterStatement();
                }
            }
        }
Exemple #2
0
        protected virtual async Task DropTemporaryTableIfNecessaryAsync(IQueryable persister, ISessionImplementor session, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (Factory.Dialect.DropTemporaryTableAfterUse())
            {
                IIsolatedWork work = new TmpIdTableDropIsolatedWork(persister, log, session);

                if (ShouldIsolateTemporaryTableDDL())
                {
                    session.ConnectionManager.Transaction.RegisterSynchronization(new AfterTransactionCompletes((success) =>
                    {
                        if (Factory.Settings.IsDataDefinitionInTransactionSupported)
                        {
                            Isolater.DoIsolatedWork(work, session);
                        }
                        else
                        {
                            Isolater.DoNonTransactedWork(work, session);
                        }
                    }));
                }
                else
                {
                    using (var dummyCommand = await(session.ConnectionManager.CreateCommandAsync(cancellationToken)).ConfigureAwait(false))
                    {
                        await(work.DoWorkAsync(dummyCommand.Connection, dummyCommand.Transaction, cancellationToken)).ConfigureAwait(false);
                        session.ConnectionManager.AfterStatement();
                    }
                }
            }
            else
            {
                // at the very least cleanup the data :)
                DbCommand ps = null;
                try
                {
                    var commandText = new SqlString("delete from " + persister.TemporaryIdTableName);
                    ps = await(session.Batcher.PrepareCommandAsync(CommandType.Text, commandText, new SqlType[0], cancellationToken)).ConfigureAwait(false);
                    await(session.Batcher.ExecuteNonQueryAsync(ps, cancellationToken)).ConfigureAwait(false);
                }
                catch (Exception t)
                {
                    log.Warn(t, "unable to cleanup temporary id table after use [{0}]", t);
                }
                finally
                {
                    if (ps != null)
                    {
                        try
                        {
                            session.Batcher.CloseCommand(ps, null);
                        }
                        catch (Exception)
                        {
                            // ignore
                        }
                    }
                }
            }
        }
        /// <summary> Suspend the current transaction and perform work in a new transaction</summary>
        public virtual object DoWorkInNewTransaction(ISessionImplementor session)
        {
            Work work = new Work(session, this);

            Isolater.DoIsolatedWork(work, session);
            return(work.generatedValue);
        }
        protected virtual void DropTemporaryTableIfNecessary(IQueryable persister, ISessionImplementor session)
        {
            if (Factory.Dialect.DropTemporaryTableAfterUse())
            {
                IIsolatedWork work = new TmpIdTableDropIsolatedWork(persister, log, session);

                if (ShouldIsolateTemporaryTableDDL())
                {
                    session.ConnectionManager.Transaction.RegisterSynchronization(new AfterTransactionCompletes((success) =>
                    {
                        if (Factory.Settings.IsDataDefinitionInTransactionSupported)
                        {
                            Isolater.DoIsolatedWork(work, session);
                        }
                        else
                        {
                            Isolater.DoNonTransactedWork(work, session);
                        }
                    }));
                }
                else
                {
                    using (var dummyCommand = session.ConnectionManager.CreateCommand())
                    {
                        work.DoWork(dummyCommand.Connection, dummyCommand.Transaction);
                        session.ConnectionManager.AfterStatement();
                    }
                }
            }
            else
            {
                // at the very least cleanup the data :)
                DbCommand ps = null;
                try
                {
                    var commandText = new SqlString("delete from " + persister.TemporaryIdTableName);
                    ps = session.Batcher.PrepareCommand(CommandType.Text, commandText, new SqlType[0]);
                    session.Batcher.ExecuteNonQuery(ps);
                }
                catch (Exception t)
                {
                    log.Warn("unable to cleanup temporary id table after use [" + t + "]");
                }
                finally
                {
                    if (ps != null)
                    {
                        try
                        {
                            session.Batcher.CloseCommand(ps, null);
                        }
                        catch (Exception)
                        {
                            // ignore
                        }
                    }
                }
            }
        }
        public void TenantIsolatedWorkOpensTenantConnection()
        {
            if (!IsSqlServerDialect)
            {
                Assert.Ignore("MSSqlServer specific test");
            }

            using (var ses = OpenTenantSession("tenant1"))
            {
                Isolater.DoIsolatedWork(new TenantIsolatatedWork("tenant1"), ses.GetSessionImplementation());
            }
        }