Example #1
0
        public int AddOrder(Order order)
        {
            try
            {
                _db.BeginTransaction();
                // Do transacted updates here
                var poco         = _orderMapper.MapToPoco(order);
                var insertResult = _db.Insert("Orders", "Id", poco);
                var id           = Convert.ToInt32(insertResult);
                foreach (var orderedMeal in poco.OrderedMeals)
                {
                    orderedMeal.OrderId = id;
                    _db.Insert("OrderedMeals", "Id", orderedMeal);
                }

                // Commit
                _db.CompleteTransaction();
                return(id);
            }
            catch (Exception)
            {
                _db.AbortTransaction();
                throw;
            }
        }
Example #2
0
        public IDatabaseSyntax ExecuteEmbeddedScript(string resourceName)
        {
            var script = _resourceHelper.GetText(resourceName);

            _database.BeginTransaction();
            try {
                foreach (var part in Regex.Split(script, @"[\r\n]+\s*GO\s*[\r\n]+", RegexOptions.IgnoreCase))
                {
                    try {
                        using (var command = _database.CreateCommand(_database.Connection, "")) {
                            // might take quite a bit of time on a large instance
                            command.CommandTimeout = TimeSpan.FromDays(1).Seconds;
                            command.CommandText    = part;
                            command.CommandType    = CommandType.Text;
                            command.ExecuteNonQuery();
                        }
                    }
                    catch (Exception ex) {
                        throw new UmbracoMigrationException("Failed to execute script (" + ex.Message + "):\r\n" + part, ex);
                    }
                }
            }
            catch (Exception) {
                _database.AbortTransaction();
                throw;
            }
            finally {
                _database.CompleteTransaction();
            }

            return(this);
        }
        public void LeakTest()
        {
            _database = DatabaseContext.Database;           // creates a database

            _database.Execute("CREATE TABLE foo (id INT)"); // opens a connection
            Assert.IsNull(_database.Connection);            // is immediately closed

            _database.BeginTransaction();                   // opens and maintains a connection

            // the test is leaking a scope with a non-null database
            var contextGuid = CallContext.LogicalGetData(ScopeProvider.ScopeItemKey).AsGuid();

            Assert.AreNotEqual(Guid.Empty, contextGuid);

            // only if Core.DEBUG_SCOPES are defined
            //var contextScope = DatabaseContext.ScopeProvider.CallContextObjects[contextGuid] as NoScope;
            //Assert.IsNotNull(contextScope);
            //Assert.IsNotNull(contextScope.DatabaseOrNull);
            //Assert.AreSame(_database, contextScope.DatabaseOrNull);

            // save the connection
            _connection = _database.Connection;
            Assert.IsInstanceOf <StackExchange.Profiling.Data.ProfiledDbConnection>(_connection);
            _connection = ((StackExchange.Profiling.Data.ProfiledDbConnection)_connection).InnerConnection;

            // the connection is open
            Assert.IsNotNull(_connection);
            Assert.AreEqual(ConnectionState.Open, _connection.State);
        }