Skip to content

Commit

Permalink
Added SQL Server transaction support
Browse files Browse the repository at this point in the history
  • Loading branch information
ctacke committed Mar 7, 2018
1 parent 325d3ab commit 7ab5e9b
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Core/DataStore.cs
Expand Up @@ -700,7 +700,7 @@ protected object GetInstanceValue(FieldAttribute field, object instance)
}

public virtual void BeginTransaction()
{
{
BeginTransaction(IsolationLevel.Unspecified);
}

Expand Down
37 changes: 37 additions & 0 deletions SQL Server/SqlServerDataStore.cs
Expand Up @@ -20,6 +20,8 @@ public partial class SqlServerDataStore : SQLStoreBase<SqlEntityInfo>, IDisposab

public const int DefaultServerPort = 1433;
public const ConnectionBehavior DefaultConnectionBehavior = ConnectionBehavior.AlwaysNew;
private IDbConnection m_transactionConnection = null;
private IDbTransaction m_transaction = null;

public SqlServerDataStore(SqlConnectionInfo info)
: base()
Expand Down Expand Up @@ -434,6 +436,41 @@ public override void OnInsert(object item, bool insertReferences)
}
}

public override void BeginTransaction()
{
if (m_transactionConnection != null)
{
throw new Exception("Transaction already in progress");
}

m_transactionConnection = GetConnection(false);
m_transaction = m_transactionConnection.BeginTransaction(IsolationLevel.ReadCommitted);
}

public override void Commit()
{
if (m_transaction == null)
{
throw new Exception("No active transaction");
}
m_transaction.Commit();
m_transaction = null;
DoneWithConnection(m_transactionConnection, false);
m_transactionConnection = null;
}

public override void Rollback()
{
if (m_transaction == null)
{
throw new Exception("No active transaction");
}
m_transaction.Rollback();
m_transaction = null;
DoneWithConnection(m_transactionConnection, false);
m_transactionConnection = null;
}

private int GetIdentity(IDbConnection connection)
{
using (var command = new SqlCommand("SELECT @@IDENTITY", connection as SqlConnection))
Expand Down
4 changes: 2 additions & 2 deletions Shared/OpenNETCF.ORM.Version.cs
Expand Up @@ -9,5 +9,5 @@
[assembly: AssemblyProduct("OpenNETCF.ORM")]
[assembly: AssemblyCopyright("Copyright © Chris Tacke 2010-2017")]

[assembly: AssemblyVersion("1.0.18058.0")]
[assembly: AssemblyFileVersion("1.0.18058.0")]
[assembly: AssemblyVersion("1.0.18065.0")]
[assembly: AssemblyFileVersion("1.0.18065.0")]
Expand Up @@ -59,6 +59,54 @@ public void BaseClassTest()

}

[TestMethod]
public void TransactionTest()
{
var connection = new SqlConnectionInfo();
connection.DatabaseName = "TEST";
connection.ServerName = "test.opennetcf.com";
connection.ServerPort = 1433;
connection.UserName = "TEST";
connection.Password = "TEST!";

var store = new SqlServerDataStore(connection);

try
{
store.AddType<PublishedTenantBuildingState>();
store.AddType<PublishedTenantApartmentState>();

var b = new PublishedTenantBuildingState()
{
PublishID = Guid.NewGuid(),
RecordDateUtc = DateTime.Now.ToUniversalTime()
};
var a1 = new PublishedTenantApartmentState()
{
PublishID = Guid.NewGuid(),
PublishedBuildingStateID = b.PublishID,
SpaceTemperature = 1
};
var a2 = new PublishedTenantApartmentState()
{
PublishID = Guid.NewGuid(),
PublishedBuildingStateID = b.PublishID,
SpaceTemperature = 2
};


store.BeginTransaction();
store.Insert(b);
store.Insert(a1);
store.Insert(a2);
store.Commit();
}
catch (Exception ex)
{
}

}

[TestMethod]
public void GuidPKTest()
{
Expand Down
2 changes: 1 addition & 1 deletion nuspec/opennetcf-orm-sqlserver.nuspec
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>opennetcf-orm-sqlserver</id>
<version>1.0.18058.0</version>
<version>1.0.18065.0</version>
<title>OpenNETCF ORM for Microsoft SQL Server</title>
<authors>Chris Tacke</authors>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
Expand Down

0 comments on commit 7ab5e9b

Please sign in to comment.