public object PerformInsert(SqlCommandInfo insertSQL, ISessionImplementor session, IBinder binder)
        {
            try
            {
                // prepare and execute the insert
                IDbCommand insert = session.Batcher.PrepareCommand(insertSQL.CommandType, insertSQL.Text, insertSQL.ParameterTypes);
                try
                {
                    binder.BindValues(insert);
                    session.Batcher.ExecuteNonQuery(insert);
                }
                finally
                {
                    session.Batcher.CloseCommand(insert, null);
                }
            }
            catch (DbException sqle)
            {
                throw ADOExceptionHelper.Convert(session.Factory.SQLExceptionConverter, sqle,
                                                 "could not insert: " + persister.GetInfoString(), insertSQL.Text);
            }

            SqlString selectSQL = SelectSQL;

            using (new SessionIdLoggingContext(session.SessionId))
                try
                {
                    //fetch the generated id in a separate query
                    IDbCommand idSelect = session.Batcher.PrepareCommand(CommandType.Text, selectSQL, ParametersTypes);
                    try
                    {
                        BindParameters(session, idSelect, binder.Entity);
                        IDataReader rs = session.Batcher.ExecuteReader(idSelect);
                        try
                        {
                            return(GetResult(session, rs, binder.Entity));
                        }
                        finally
                        {
                            session.Batcher.CloseReader(rs);
                        }
                    }
                    finally
                    {
                        session.Batcher.CloseCommand(idSelect, null);
                    }
                }
                catch (DbException sqle)
                {
                    throw ADOExceptionHelper.Convert(session.Factory.SQLExceptionConverter, sqle,
                                                     "could not retrieve generated id after insert: " + persister.GetInfoString(),
                                                     insertSQL.Text);
                }
        }
Esempio n. 2
0
 public object PerformInsert(SqlCommandInfo insertSQL, ISessionImplementor session, IBinder binder)
 {
     try
     {
         // prepare and execute the insert
         IDbCommand insert = Prepare(insertSQL, session);
         try
         {
             binder.BindValues(insert);
             return(ExecuteAndExtract(insert, session));
         }
         finally
         {
             ReleaseStatement(insert, session);
         }
     }
     catch (DbException sqle)
     {
         throw ADOExceptionHelper.Convert(session.Factory.SQLExceptionConverter, sqle,
                                          "could not insert: " + persister.GetInfoString(), insertSQL.Text);
     }
 }
        public object PerformInsert(SqlCommandInfo insertSql, ISessionImplementor session, IBinder binder)
        {
            // NH-2145: Prevent connection releases between insert and select when we cannot perform
            // them as a single statement. Retrieving id most of the time relies on using the same connection.
            session.ConnectionManager.FlushBeginning();
            try
            {
                try
                {
                    // prepare and execute the insert
                    var insert = session.Batcher.PrepareCommand(insertSql.CommandType, insertSql.Text, insertSql.ParameterTypes);
                    try
                    {
                        binder.BindValues(insert);
                        session.Batcher.ExecuteNonQuery(insert);
                    }
                    finally
                    {
                        session.Batcher.CloseCommand(insert, null);
                    }
                }
                catch (DbException sqle)
                {
                    throw ADOExceptionHelper.Convert(session.Factory.SQLExceptionConverter, sqle,
                                                     "could not insert: " + persister.GetInfoString(), insertSql.Text);
                }

                var selectSql = SelectSQL;
                using (session.BeginProcess())
                {
                    try
                    {
                        //fetch the generated id in a separate query
                        var idSelect = session.Batcher.PrepareCommand(CommandType.Text, selectSql, ParametersTypes);
                        try
                        {
                            BindParameters(session, idSelect, binder);
                            var rs = session.Batcher.ExecuteReader(idSelect);
                            try
                            {
                                return(GetResult(session, rs, binder.Entity));
                            }
                            finally
                            {
                                session.Batcher.CloseReader(rs);
                            }
                        }
                        finally
                        {
                            session.Batcher.CloseCommand(idSelect, null);
                        }
                    }
                    catch (DbException sqle)
                    {
                        throw ADOExceptionHelper.Convert(session.Factory.SQLExceptionConverter, sqle,
                                                         "could not retrieve generated id after insert: " + persister.GetInfoString(),
                                                         insertSql.Text);
                    }
                }
            }
            finally
            {
                session.ConnectionManager.FlushEnding();
            }
        }