예제 #1
0
 protected sealed override void DoIteration()
 {
     if (_iteration % _iterations == 1)
     { // Emulating new session
         _objectKey = Guid.NewGuid();
         _cmdLoad.Parameters[0].Value = _cmdSave.Parameters[0].Value = _objectKey;
     }
     else
     { // Loading data from db
         // Step 1: Getting serialized data
         _cmdLoad.Parameters[0].Value = _objectKey;
         _cmdLoad.ExecuteNonQuery();
         byte[] obj = (byte[])_cmdLoad.Parameters[1].Value;
         if (obj == null)
         {
             throw new Exception("Cannot locate an object with key: " + _objectKey.ToString());
         }
         SessionObject loadedObj = ObjStoreUtils.Deserialize <SessionObject>(obj);
         // Validation - for demo purposes
         if (loadedObj.iteration != _obj.iteration)
         {
             throw new Exception("Validation failed: Iterations do not match");
         }
     }
     // Saving object to DB
     _obj.iteration = _iteration;
     _cmdSave.Parameters[2].Value = ObjStoreUtils.Serialize <SessionObject>(_obj);
     _cmdSave.ExecuteNonQuery();
 }
 protected sealed override void DoIteration()
 {
     if (_iteration % _iterations == 1)
     { // Emulating new session
         _objectKey = Guid.NewGuid();
         _cmdLoad.Parameters[0].Value = _cmdSave.Parameters[0].Value = _objectKey;
     }
     else
     { // Loading data from db
         // Step 1: Getting serialized chunks
         _cmdLoad.Parameters[0].Value = _objectKey;
         var chunks = new List <byte[]>();
         using (var reader = _cmdLoad.ExecuteReader())
         {
             while (reader.Read())
             {
                 chunks.Add((byte[])reader[0]);
             }
         }
         if (chunks.Count == 0)
         {
             throw new Exception("Cannot locate an object with key: " + _objectKey.ToString());
         }
         // Step 2: Deserializing
         SessionObject loadedObj = ObjStoreUtils.Deserialize <SessionObject>(ObjStoreUtils.Merge(chunks));
         // Validation - for demo purposes
         if (loadedObj.iteration != _obj.iteration)
         {
             throw new Exception("Validation failed: Iterations do not match");
         }
     }
     // Saving object to DB
     _obj.iteration = _iteration;
     byte[] serializedObj = ObjStoreUtils.Serialize <SessionObject>(_obj);
     if (_useTVP)
     {
         DataTable tbl = new DataTable();
         tbl.Columns.Add("ChunkNum", typeof(short));
         tbl.Columns.Add("Data", typeof(byte[]));
         var chunks = ObjStoreUtils.Split(serializedObj, 8000);
         for (int i = 0; i < chunks.Count; i++)
         {
             tbl.Rows.Add(i + 1, chunks[i]);
         }
         _cmdSave.Parameters[2].Value = tbl;
     }
     else
     {
         _cmdSave.Parameters[2].Value = serializedObj;
     }
     _cmdSave.ExecuteNonQuery();
 }