private IDbCommand CommandGetAndGetLock(IDbConnection conn, bool getAndLock, string sessionId)
        {
            var getSql     = "TempGetStateItem";
            var getLockSql = "TempGetStateItemExclusive";
            var e          = DatabaseServices.ExecutionService;

            var cmd = e.CreateCommand(conn, getAndLock ? getLockSql : getSql);

            cmd.CommandType    = CommandType.StoredProcedure;
            cmd.CommandTimeout = queryTimeout;

            e.CreateParameter(cmd, P.P("id"), DbType.StringFixedLength, sessionId).Size = SessionConstants.ID_LENGTH;
            var p = e.CreateParameter(cmd, P.P("item"), DbType.Binary, System.DBNull.Value);

            MySQLParams.SetBlobType(p);
            p.Direction = ParameterDirection.Output;
            p           = e.CreateParameter(cmd, P.P("locked"), DbType.Boolean, System.DBNull.Value);
            p.Direction = ParameterDirection.Output;
            p           = e.CreateParameter(cmd, P.P("lockAge"), DbType.Int32, System.DBNull.Value);
            p.Direction = ParameterDirection.Output;
            p           = e.CreateParameter(cmd, P.P("lockCookie"), DbType.Int32, System.DBNull.Value);
            p.Direction = ParameterDirection.Output;
            p           = e.CreateParameter(cmd, P.P("actionFlags"), DbType.Int32, System.DBNull.Value);
            p.Direction = ParameterDirection.Output;

            return(cmd);
        }
        private byte[] GetAndGetAndLock(bool getAndLock, string sessionId, out int lockId, out bool locked, out TimeSpan lockAge, out ItemState state)
        {
            lockId  = -1;
            locked  = false;
            lockAge = TimeSpan.Zero;
            state   = default(ItemState);

            using (var conn = DatabaseServices.TransactionService.CreateConnection()) {
                byte[] buf = null;

                using (var cmd = CommandGetAndGetLock(conn, getAndLock, sessionId)) {
                    using (var reader = cmd.ExecuteReader()) {
                        if (reader.Read())
                        {
                            buf = (byte[])reader[0];
                        }
                    }

                    if (MySQLParams.IsNull(cmd.Parameters[2]))
                    {
                        return(null);
                    }

                    locked = MySQLParams.BoolVal(cmd.Parameters[2]);
                    lockId = MySQLParams.IntVal(cmd.Parameters[4]);

                    if (locked)
                    {
                        lockAge = MySQLParams.TimeSpanVal(cmd.Parameters[3]);

                        if (lockAge > MySQLTimeSpans.Year)
                        {
                            lockAge = TimeSpan.Zero;
                        }

                        return(null);
                    }

                    state = MySQLParams.ItemStateVal(cmd.Parameters[5]);

                    if (buf == null)
                    {
                        buf = MySQLParams.BinaryVal(cmd, cmd.Parameters[1]);
                    }

                    return(buf);
                }
            }
        }
 public void Insert(string sessionId, byte[] data, TimeSpan timeout)
 {
     try {
         using (var conn = DatabaseServices.TransactionService.CreateConnection()) {
             var e = DatabaseServices.ExecutionService;
             using (var cmd = InsertCommand(conn)) {
                 MySQLParams.SetVal(cmd.Parameters[0], sessionId);
                 MySQLParams.SetVal(cmd.Parameters[1], data);
                 MySQLParams.SetVal(cmd.Parameters[2], Convert.ToInt32(timeout.TotalMinutes));
                 cmd.ExecuteNonQuery();
             }
         }
     } catch (Exception e) {
         throw new SessionStoreException(e.Message, e);
     }
 }
        private IDbCommand InsertCommand(IDbConnection conn)
        {
            var e   = DatabaseServices.ExecutionService;
            var sql = "TempInsertStateItem";
            var cmd = e.CreateCommand(conn, sql);

            cmd.CommandType    = CommandType.StoredProcedure;
            cmd.CommandTimeout = queryTimeout;
            e.CreateParameter(cmd, P.P("id"), DbType.StringFixedLength, null).Size = SessionConstants.ID_LENGTH;
            var p = e.CreateParameter(cmd, P.P("item"), DbType.Binary, null);

            MySQLParams.SetBlobType(p);
            e.CreateParameter(cmd, P.P("timeout"), DbType.Int32, null);

            return(cmd);
        }
 public void InsertOrUpdate(string sessionId, int lockId, byte[] data, TimeSpan timeout)
 {
     try {
         using (var con = DatabaseServices.TransactionService.CreateConnection()) {
             var e = DatabaseServices.ExecutionService;
             using (var cmd = e.CreateCommand(con, "TempCreateOrUpdateStateItem")) {
                 cmd.CommandTimeout = queryTimeout;
                 cmd.CommandType    = CommandType.StoredProcedure;
                 e.CreateParameter(cmd, P.P("id"), DbType.StringFixedLength, sessionId).Size = SessionConstants.ID_LENGTH;
                 var p = e.CreateParameter(cmd, P.P("item"), DbType.Binary, data);
                 MySQLParams.SetBlobType(p);
                 e.CreateParameter(cmd, P.P("timeout"), DbType.Int32, Convert.ToInt32(timeout.TotalMinutes));
                 e.CreateParameter(cmd, P.P("lockCookie"), DbType.Int32, lockId);
                 cmd.ExecuteNonQuery();
             }
         }
     } catch (Exception e) {
         throw new SessionStoreException(e.Message, e);
     }
 }