Esempio n. 1
0
        public static void Init(TestContext context)
        {
            const string createConnection = "Server=localhost;username=cassandra;password=cassandra";

            const string createKsCql =
                @"CREATE KEYSPACE LinqTest WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 1} and durable_writes = 'false';";
            const string createTableCql =
                @"create table linqtest.myvalue (id int primary key, value text, ignored text);";


            using (var connection = new CqlConnection(createConnection))
            {
                connection.SetConnectionTimeout(0);
                connection.Open();

                try
                {
                    var createKs = new CqlCommand(connection, createKsCql);
                    createKs.ExecuteNonQuery();

                    var createTable = new CqlCommand(connection, createTableCql);
                    createTable.ExecuteNonQuery();

                    using (var transaction = connection.BeginTransaction())
                    {
                        transaction.BatchType = CqlBatchType.Unlogged;

                        var insert = new CqlCommand(connection, "insert into linqtest.myvalue (id,value) values(?,?)");
                        insert.Transaction = transaction;
                        insert.Prepare();

                        for (int i = 0; i < 10000; i++)
                        {
                            insert.Parameters[0].Value = i;
                            insert.Parameters[1].Value = "Hallo " + i;
                            insert.ExecuteNonQuery();
                        }

                        transaction.Commit();
                    }
                }
                catch (AlreadyExistsException)
                {
                    //ignore
                }
            }

            CqlConnection.Shutdown(createConnection);
        }
Esempio n. 2
0
        static void SetupDatabase()
        {

            const string createKsCql =
                @"CREATE KEYSPACE linqperftest WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 1} and durable_writes = 'false';";

            const string createTableCql =
                @"create table linqperftest.myvalue (id int primary key, value text, ignored text);";

            using (var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();

                try
                {
                    var createKs = new CqlCommand(connection, createKsCql);
                    createKs.ExecuteNonQuery();

                    var createTable = new CqlCommand(connection, createTableCql);
                    createTable.ExecuteNonQuery();

                    using (var transaction = connection.BeginTransaction())
                    {
                        transaction.BatchType = CqlBatchType.Unlogged;

                        var insert = new CqlCommand(connection, "insert into linqperftest.myvalue (id,value) values(?,?)");
                        insert.Transaction = transaction;
                        insert.Prepare();

                        for (int i = 0; i < 10000; i++)
                        {
                            insert.Parameters[0].Value = i;
                            insert.Parameters[1].Value = "Hallo " + i;
                            insert.ExecuteNonQuery();
                        }

                        transaction.Commit();
                    }

                }
                catch (AlreadyExistsException)
                {
                }
            }
        }
Esempio n. 3
0
        private async Task BatchInsertInternal(CqlBatchType batchType)
        {
            //Assume
            const string insertCql = @"insert into Test.BasicFlow (id,value) values (?,?);";
            const string retrieveCql = @"select id,value,ignored from Test.BasicFlow;";

            //Act
            using(var connection = new CqlConnection(ConnectionString))
            {
                await connection.OpenAsync();
                var transaction = connection.BeginTransaction();
                transaction.BatchType = batchType;

                //insert data
                var cmd = new CqlCommand(connection, insertCql, CqlConsistency.One);
                cmd.Transaction = transaction;

                await cmd.PrepareAsync();

                for(int i = 0; i < 10; i++)
                {
                    cmd.Parameters[0].Value = i;
                    cmd.Parameters[1].Value = "Hello " + i;
                    await cmd.ExecuteNonQueryAsync();
                }

                var cmd2 = new CqlCommand(connection, insertCql, CqlConsistency.One);
                cmd2.Transaction = transaction;
                cmd2.Parameters.Add("id", CqlType.Int);
                cmd2.Parameters.Add("value", CqlType.Text);

                for(int i = 10; i < 20; i++)
                {
                    cmd2.Parameters[0].Value = i;
                    cmd2.Parameters[1].Value = "Hello " + i;
                    await cmd2.ExecuteNonQueryAsync();
                }

                try
                {
                    await transaction.CommitAsync();
                }
                catch(ProtocolException pex)
                {
                    //skip when server version is below 2.0.0
                    if(pex.Code == ErrorCode.Protocol &&
                       String.Compare(connection.ServerVersion, "2.0.0", StringComparison.Ordinal) < 0)
                        return;

                    throw;
                }

                //select data
                var selectCmd = new CqlCommand(connection, retrieveCql, CqlConsistency.One);
                CqlDataReader reader = await selectCmd.ExecuteReaderAsync();
                Assert.AreEqual(20, reader.Count);

                var results = new bool[20];
                for(int i = 0; i < 20; i++)
                {
                    if(await reader.ReadAsync())
                    {
                        results[(int)reader["id"]] = true;
                        Assert.AreEqual("Hello " + reader["id"], reader["value"]);
                    }
                    else
                        Assert.Fail("Read should have succeeded");
                    }

                Assert.IsTrue(results.All(p => p));

                Assert.IsNotNull(transaction.LastBatchResult);
            }
        }
Esempio n. 4
0
        public void TransactionResetAfterDispose()
        {
            const string insertCql = @"insert into Test.BasicFlow (id,value) values (7000, 'Transaction 7000');";
            const string insertCql2 = @"insert into Test.BasicFlow (id,value) values (7001, 'Transaction 7001');";

            //Act
            using(var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();

                //skip if server version too low
                if(string.CompareOrdinal(connection.ServerVersion, "2.0.0") < 0)
                    return;

                //create transaction
                var transaction = connection.BeginTransaction();

                //insert data
                var cmd = new CqlCommand(connection, insertCql, CqlConsistency.One);
                cmd.Transaction = transaction;
                cmd.ExecuteNonQuery();

                //commit
                transaction.Commit();

                //dispose
                transaction.Dispose();

                //reset transaction to empty state, such that it can be reused
                transaction.Reset();

                //add again
                var cmd2 = new CqlCommand(connection, insertCql2, CqlConsistency.One);
                cmd2.Transaction = transaction;
                cmd2.ExecuteNonQuery();

                transaction.Commit();

                transaction.Dispose();
            }
        }
Esempio n. 5
0
        public void TransactionCommitAfterRollbackThrowsException()
        {
            const string insertCql = @"insert into Test.BasicFlow (id,value) values (8000, 'Transaction 8000');";

            //Act
            using(var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();

                //create transaction
                using(var transaction = connection.BeginTransaction())
                {
                    //insert data
                    var cmd = new CqlCommand(connection, insertCql, CqlConsistency.One);
                    cmd.Transaction = transaction;
                    cmd.ExecuteNonQuery();

                    //rollback
                    transaction.Rollback();

                    //commit -> throws error
                    transaction.Commit();
                }
            }
        }
Esempio n. 6
0
        public void TransactionRollbackAfterCommitThrowsException()
        {
            const string insertCql = @"insert into Test.BasicFlow (id,value) values (8000, 'Transaction 8000');";

            //Act
            using(var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();

                //skip if server version too low
                if(string.CompareOrdinal(connection.ServerVersion, "2.0.0") < 0)
                    throw new InvalidOperationException("as expected by test");

                //create transaction
                using(var transaction = connection.BeginTransaction())
                {
                    //insert data
                    var cmd = new CqlCommand(connection, insertCql, CqlConsistency.One);
                    cmd.Transaction = transaction;
                    cmd.ExecuteNonQuery();

                    //commit
                    transaction.Commit();

                    //rollback -> throws error
                    transaction.Rollback();
                }
            }
        }
Esempio n. 7
0
        public void TransactionAddAfterDisposeThrowsException()
        {
            const string insertCql = @"insert into Test.BasicFlow (id,value) values (6000, 'Transaction 6000');";
            const string insertCql2 = @"insert into Test.BasicFlow (id,value) values (6001, 'Transaction 6001');";

            //Act
            using(var connection = new CqlConnection(ConnectionString))
            {
                connection.Open();

                //skip if server version too low
                if(string.CompareOrdinal(connection.ServerVersion, "2.0.0") < 0)
                    throw new ObjectDisposedException("dummy"); //as expected for this test

                //create transaction
                var transaction = connection.BeginTransaction();

                //insert data
                var cmd = new CqlCommand(connection, insertCql, CqlConsistency.One);
                cmd.Transaction = transaction;
                cmd.ExecuteNonQuery();

                //commit
                transaction.Commit();

                //dispose
                transaction.Dispose();

                //add again
                var cmd2 = new CqlCommand(connection, insertCql2, CqlConsistency.One);
                cmd2.Transaction = transaction;
                cmd2.ExecuteNonQuery();
            }
        }
Esempio n. 8
0
        public async Task TransactionRolledBackDoesNotInsertData()
        {
            const string insertCql = @"insert into Test.BasicFlow (id,value) values (9876, 'Transaction 9876');";
            const string retrieveCql = @"select id from Test.BasicFlow where id=9876;";

            //Act
            using(var connection = new CqlConnection(ConnectionString))
            {
                await connection.OpenAsync();

                //create transaction
                using(var transaction = connection.BeginTransaction())
                {
                    //insert data
                    var cmd = new CqlCommand(connection, insertCql, CqlConsistency.One);
                    cmd.Transaction = transaction;
                    await cmd.ExecuteNonQueryAsync();

                    transaction.Rollback();
                }

                //check if data exists
                var selectCmd = new CqlCommand(connection, retrieveCql, CqlConsistency.One);
                using(CqlDataReader reader = await selectCmd.ExecuteReaderAsync())
                {
                    //check if any rows are returned
                    Assert.IsFalse(reader.HasRows);
                }
            }
        }
Esempio n. 9
0
        public async Task TransactionEmptyDoesNothing()
        {
            //Act
            using(var connection = new CqlConnection(ConnectionString))
            {
                await connection.OpenAsync();

                //create transaction
                using(var transaction = connection.BeginTransaction())
                {
                    //no-op, no methods added

                    await transaction.CommitAsync();
                }
            }
        }
Esempio n. 10
0
        public void UpdateInExternalTransaction()
        {
            using (var connection = new CqlConnection(ConnectionString))
            using (var transaction = connection.BeginTransaction())
            using (var context = new MyContext(connection, false))
            {
                context.Database.Log = cql => Debug.WriteLine("EXECUTE QUERY: " + cql);

                context.Database.UseTransaction(transaction);

                var value = context.Values.Find(500);
                value.Value = "Hallo daar!";
                context.SaveChanges(false);

                var command = new CqlCommand(connection, "update myvalue set value='adjusted' where id=500");
                command.Transaction = transaction;
                command.ExecuteNonQuery();

                transaction.Commit();

                //accept all changes only after commit
                context.AcceptAllChanges();
            }

            using (var context = new MyContext(ConnectionString))
            {
                var value = context.Values.Find(500);
                Assert.IsNotNull(value);
                Assert.AreEqual("adjusted", value.Value);
            }
        }