MySQL Transactions
-
- Posts: 10
- Joined: Fri 03 Apr 2009 08:39
MySQL Transactions
Hi,
I´m using the Entity Framework with the latest dotconnect driver (5.00.26).
I´ve noticed, that changes are not applied in a transaction.
Even if I create a MySqlConnection "by hand" and starting a transaction, all changes are applied on a new connection.
When there are 3 changes, the mysqld-querylog looks like this:
connection-id command
1000 an insert command
1001 another insert
1002 an update
How to fix this?
Regards
I´m using the Entity Framework with the latest dotconnect driver (5.00.26).
I´ve noticed, that changes are not applied in a transaction.
Even if I create a MySqlConnection "by hand" and starting a transaction, all changes are applied on a new connection.
When there are 3 changes, the mysqld-querylog looks like this:
connection-id command
1000 an insert command
1001 another insert
1002 an update
How to fix this?
Regards
-
- Posts: 10
- Joined: Fri 03 Apr 2009 08:39
Hello,
First of all, I´m talking about Linq to Sql and not the Entity Framework (that was another Test-Run).
My Code:
customerDataContext dataContext = new customerDataContext(connectionString);
Contract contract = new Contract();
contract.CustomerDataId = 1;
contract.ContractStatusId = 1;
contract.ProductMainId = 1;
dataContext.Contracts.InsertOnSubmit(contract);
Contract contract2 = new Contract();
contract2.CustomerDataId = 1;
contract2.ContractStatusId = 1;
contract2.ProductMainId = 1;
dataContext.Contracts.InsertOnSubmit(contract2);
dataContext.SubmitChanges();
The SQL-Queries are ok, but they are on different Connections and without any Transaction.
Even if I create the connection manually
MySqlConnection mySqlConnection = new MySqlConnection(connectionString);
mySqlConnection.Open();
mySqlConnection.BeginTransaction();
customerDataContext dataContext = new customerDataContext(mySqlConnection);
This starts a Transaction on the first connection, but the queries are executed on others.
I´ve also noticed, that accessing dataContext.Connection throws a NotImplementedException
var foo = dataContext.Connection; // <- Exception
First of all, I´m talking about Linq to Sql and not the Entity Framework (that was another Test-Run).
My Code:
customerDataContext dataContext = new customerDataContext(connectionString);
Contract contract = new Contract();
contract.CustomerDataId = 1;
contract.ContractStatusId = 1;
contract.ProductMainId = 1;
dataContext.Contracts.InsertOnSubmit(contract);
Contract contract2 = new Contract();
contract2.CustomerDataId = 1;
contract2.ContractStatusId = 1;
contract2.ProductMainId = 1;
dataContext.Contracts.InsertOnSubmit(contract2);
dataContext.SubmitChanges();
The SQL-Queries are ok, but they are on different Connections and without any Transaction.
Even if I create the connection manually
MySqlConnection mySqlConnection = new MySqlConnection(connectionString);
mySqlConnection.Open();
mySqlConnection.BeginTransaction();
customerDataContext dataContext = new customerDataContext(mySqlConnection);
This starts a Transaction on the first connection, but the queries are executed on others.
I´ve also noticed, that accessing dataContext.Connection throws a NotImplementedException
var foo = dataContext.Connection; // <- Exception
We have no technical possibility to provide full support for the scenario with single local transaction.
It is related to the fact that MySQL connections do not support multiple opened readers simultaneously (that's why a new connection is created).
We are working on implementing the TransactionScope class support for MySQL, this will enable the behaviour you describe.
But if you are sure that your code does not use multiple readers you can work with the DataContext.Connection.Transaction property.
This is implemented in dotConnect for MySQL 5.20.24 Beta.
It is related to the fact that MySQL connections do not support multiple opened readers simultaneously (that's why a new connection is created).
We are working on implementing the TransactionScope class support for MySQL, this will enable the behaviour you describe.
But if you are sure that your code does not use multiple readers you can work with the DataContext.Connection.Transaction property.
This is implemented in dotConnect for MySQL 5.20.24 Beta.
-
- Posts: 10
- Joined: Fri 03 Apr 2009 08:39
I´ve installed 5.20.24 beta, but public System.Data.Common.DbConnection Connection { get; } doesn´t contain a property named Transaction.
So I´ve tried casting the Connection back to a MySqlConnection and invoke BeginTransaction/Commit/Rollback as needed, but the queries are still executed on different connections.
So I´ve tried casting the Connection back to a MySqlConnection and invoke BeginTransaction/Commit/Rollback as needed, but the queries are still executed on different connections.
-
- Posts: 10
- Joined: Fri 03 Apr 2009 08:39
-
- Posts: 10
- Joined: Fri 03 Apr 2009 08:39
I´ve looked further and found the problem.
I´ve created a Class which inherits from the DataContext and override the SubmitChanges-Method to perform various validations.
This works fine so far, but if one of the validators fetches the database, the error occurs.
For example:
I´ve a Contract object which represents a Customer contract.
This object holds a CustomerId.
The Validator tries to fetch the associated Customer for additional logic.
If everything is ok, my SubmitChanges-Method simply calls base.SubmitChanges()
This works fine with the stable version.
The beta runs the Insert-Query for the Contract and then throws the exception.
If I comment out the part in the validator, that fetches the customer-object, it works again.
I´ve added a "ChangeSet changeSet = GetChangeSet();" right before base.SubmitChanges() to look if the Customer Object is in the changeset or something like this, but there is only the new Contract Object.
I´ve created a Class which inherits from the DataContext and override the SubmitChanges-Method to perform various validations.
This works fine so far, but if one of the validators fetches the database, the error occurs.
For example:
I´ve a Contract object which represents a Customer contract.
This object holds a CustomerId.
The Validator tries to fetch the associated Customer for additional logic.
If everything is ok, my SubmitChanges-Method simply calls base.SubmitChanges()
This works fine with the stable version.
The beta runs the Insert-Query for the Contract and then throws the exception.
If I comment out the part in the validator, that fetches the customer-object, it works again.
I´ve added a "ChangeSet changeSet = GetChangeSet();" right before base.SubmitChanges() to look if the Customer Object is in the changeset or something like this, but there is only the new Contract Object.
-
- Posts: 10
- Joined: Fri 03 Apr 2009 08:39