SPRNET-1380

Introduced HibernateTxScopeTransactionManager class to coordinate rollback behavior between System.Transactions and NHibernate ITransaction instances based on changed made to NH in 2.1.2
This commit is contained in:
sbohlen
2010-10-07 20:04:41 +00:00
parent a3e74e0c36
commit b3cc1b1034
5 changed files with 1327 additions and 2 deletions

View File

@@ -26,8 +26,7 @@
<WarningLevel>4</WarningLevel>
<DocumentationFile>Spring.Data.NHibernate21.xml</DocumentationFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<NoWarn>
</NoWarn>
<NoWarn>1591</NoWarn>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
@@ -60,6 +59,7 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Transactions" />
<Reference Include="System.Web" />
<Reference Include="System.Xml" />
</ItemGroup>
@@ -170,6 +170,7 @@
<Compile Include="Data\NHibernate\DelegatingLocalSessionFactoryObject.cs" />
<Compile Include="Data\NHibernate\DelegatingSessionFactory.cs" />
<Compile Include="Data\NHibernate\SimpleDelegatingSessionFactory.cs" />
<Compile Include="Data\NHibernate\HibernateTxScopeTransactionManager.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Spring.Aop\Spring.Aop.2010.csproj">

View File

@@ -0,0 +1,139 @@
#region License
/*
* Copyright © 2002-2005 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
#region Imports
using System;
using System.Data;
using log4net;
using log4net.Config;
using NHibernate;
using NUnit.Framework;
using Spring.Context;
using Spring.Context.Support;
using Spring.Data.Common;
using Spring.Data.Support;
using Spring.Transaction;
using Spring.Transaction.Support;
using Spring.Transaction.Interceptor;
using System.Transactions;
#endregion
namespace Spring.Data.NHibernate
{
/// <summary>
/// Use of Hibernate Template against database.
/// </summary>
/// <author>Mark Pollack (.NET)</author>
[TestFixture]
public class HibernateTxScopeTransactionManagerTests
{
#region Fields
private IDbProvider dbProvider;
private IPlatformTransactionManager transactionManager;
private IApplicationContext ctx;
#endregion
#region Constants
/// <summary>
/// The shared <see cref="log4net.ILog"/> instance for this class (and derived classes).
/// </summary>
protected static readonly ILog log =
LogManager.GetLogger(typeof(TemplateTests));
//// force Spring.Data.NHibernate to be preloaded by runtime
//private Type TLocalSessionFactoryObject = typeof(LocalSessionFactoryObject);
#endregion
[SetUp]
public void SetUp()
{
//NamespaceParserRegistry.RegisterParser(typeof(DatabaseNamespaceParser));
BasicConfigurator.Configure();
string assemblyName = GetType().Assembly.GetName().Name;
ctx = new XmlApplicationContext("assembly://" + assemblyName + "/Spring.Data.NHibernate/HibernateTxScopeTransactionManagerTests.xml");
dbProvider = ctx["DbProvider"] as IDbProvider;
transactionManager = ctx["transactionManager"] as IPlatformTransactionManager;
CleanupDatabase(dbProvider.CreateConnection());
}
private static void CleanupDatabase(IDbConnection conn)
{
conn.Open();
using (conn)
{
ExecuteSql(conn, "delete credits");
ExecuteSql(conn, "delete debits");
ExecuteSql(conn, "delete TestObjects");
ExecuteSql(conn, "insert TestObjects(Age,Name) Values(5, 'Gabriel')");
}
}
private static void ExecuteSql(IDbConnection conn, string sql)
{
IDbCommand cmd;
cmd = conn.CreateCommand();
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
}
[Transaction]
private void DoSave(bool simulateException)
{
using (TransactionScope tx = new TransactionScope())
{
ISession s = ((ISessionFactory)ctx["SessionFactory"]).OpenSession();
TestObject to = new TestObject();
to.Name = "George";
to.Age = 33;
if (simulateException) { throw new Exception("Simulated Failure in Save Operation."); }
s.Save(to);
tx.Complete();
}
}
[Test]
public void CanProperlyReleaseConnectionsWhenTransactionsAreRolledBack()
{
for (int i = 0; i < 200; i++)
{
try
{
DoSave(true);
}
catch (Exception)
{
}
}
}
}
}

View File

@@ -0,0 +1,88 @@
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns='http://www.springframework.net'
xmlns:db="http://www.springframework.net/database"
xmlns:tx="http://www.springframework.net/tx">
<db:provider id="DbProvider"
provider="SqlServer-2.0"
connectionString="Data Source=SPRINGQA;Database=Spring;User ID=springqa;Password=springqa;Trusted_Connection=False;Max Pool Size=10;"/>
<object id="SessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate21">
<!-- TODO Provide dedicated NHibernate Schema -->
<property name="DbProvider" ref="DbProvider"/>
<property name="MappingResources">
<list>
<value>assembly://Spring.Data.NHibernate21.Integration.Tests/Spring.Data.NHibernate/TestObject.hbm.xml</value>
</list>
</property>
<property name="HibernateProperties">
<dictionary>
<entry key="dialect" value="NHibernate.Dialect.MsSql2000Dialect"/>
<entry key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
</dictionary>
</property>
</object>
<object id="transactionManager" type="Spring.Data.NHibernate.HibernateTxScopeTransactionManager, Spring.Data.NHibernate21">
<property name="DbProvider" ref="DbProvider"/>
<property name="sessionFactory" ref="SessionFactory"/>
</object>
<tx:attribute-driven />
<!--<object id="nativeNHTestObjectDao" type="Spring.Data.NHibernate.NativeNHTestObjectDao, Spring.Data.NHibernate21.Integration.Tests">
<property name="SessionFactory" ref="SessionFactory"/>
</object>
<object id="NHTestObjectDao" type="Spring.Data.NHibernate.NHTestObjectDao, Spring.Data.NHibernate21.Integration.Tests">
<property name="SessionFactory" ref="SessionFactory"/>
</object>-->
<!-- Transactional Proxy for TestObjectDao using transaction attributes -->
<!--<object id="testObjectDaoViaTxAttributes"
type="Spring.Transaction.Interceptor.TransactionProxyFactoryObject, Spring.Data">
<property name="PlatformTransactionManager" ref="hibernateTransactionManager"/>
<property name="Target">
<object type="Spring.Data.NHibernate.NHTestObjectDao">
<property name="SessionFactory" ref="SessionFactory"/>
</object>
</property>
<property name="ProxyInterfaces" value="Spring.Data.NHibernate.ITestObjectDao"/>
<property name="TransactionAttributeSource">
<object type="Spring.Transaction.Interceptor.AttributesTransactionAttributeSource" />
</property>
</object>
--><!-- Transactional Proxy for TestObjectDao using the TransactionProxyFactory --><!--
<object id="testObjectDaoTransProxy"
type="Spring.Transaction.Interceptor.TransactionProxyFactoryObject, Spring.Data">
<property name="PlatformTransactionManager" ref="hibernateTransactionManager"/>
<property name="Target">
<object type="Spring.Data.NHibernate.NHTestObjectDao, Spring.Data.NHibernate21.Integration.Tests">
<property name="SessionFactory" ref="SessionFactory"/>
</object>
</property>
<property name="ProxyInterfaces" value="Spring.Data.NHibernate.ITestObjectDao"/>
<property name="TransactionAttributes">
<name-values>
<add key="Create*" value="PROPAGATION_REQUIRED"/>
<add key="Delete*" value="PROPAGATION_REQUIRED"/>
<add key="Update*" value="PROPAGATION_REQUIRED"/>
<add key="Find*" value="PROPAGATION_REQUIRED"/>
</name-values>
</property>
</object>-->
</objects>

View File

@@ -144,6 +144,7 @@
<Compile Include="Data\NHibernate\Bytecode\NHibernateTestImports.cs" />
<Compile Include="Data\NHibernate\Bytecode\Product.cs" />
<Compile Include="Data\NHibernate\AccountController.cs" />
<Compile Include="Data\NHibernate\HibernateTxScopeTransactionManagerTests.cs" />
<Compile Include="Data\NHibernate\IAccountController.cs" />
<Compile Include="Setup.cs" />
</ItemGroup>
@@ -202,6 +203,7 @@
</ItemGroup>
<ItemGroup>
<Content Include="Data\NHibernate\Bytecode\Foo.Spechbm.xml" />
<EmbeddedResource Include="Data\NHibernate\HibernateTxScopeTransactionManagerTests.xml" />
<Content Include="TestEnbeddedConfig.cfg.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>