Skip to content

Commit 17c0266

Browse files
committed
Configuration for ignoring or not ignoring exceptions in IInterceptor.BeforeTransactionCompletion added.
1 parent b607041 commit 17c0266

8 files changed

+597
-516
lines changed

src/NHibernate.Test/NHSpecificTest/NH1082/Fixture.cs

+66-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Collections;
2+
using NHibernate.Cfg;
13
using NUnit.Framework;
24

35
namespace NHibernate.Test.NHSpecificTest.NH1082
@@ -10,11 +12,11 @@ public override string BugNumber
1012
get { return "NH1082"; }
1113
}
1214

13-
/* Exceptions thrown in IInterceptor.BeforeTransactionCompletion should cause the transaction to be rolled back
14-
*/
1515
[Test]
16-
public void TestBug()
16+
public void ExceptionsInBeforeTransactionCompletionAbortTransaction()
1717
{
18+
Assert.IsFalse(sessions.Settings.IsInterceptorsBeforeTransactionCompletionIgnoreExceptions);
19+
1820
C c = new C();
1921
c.ID = 1;
2022
c.Value = "value";
@@ -41,4 +43,65 @@ public void TestBug()
4143
}
4244
}
4345
}
46+
47+
[TestFixture]
48+
public class OldBehaviorEnabledFixture : TestCase
49+
{
50+
protected override IList Mappings
51+
{
52+
get
53+
{
54+
return new string[]
55+
{
56+
"NHSpecificTest.NH1082.Mappings.hbm.xml"
57+
};
58+
}
59+
}
60+
61+
protected override string MappingsAssembly
62+
{
63+
get { return "NHibernate.Test"; }
64+
}
65+
66+
protected override void Configure(Configuration configuration)
67+
{
68+
configuration.SetProperty(Environment.InterceptorsBeforeTransactionCompletionIgnoreExceptions, "true");
69+
base.Configure(configuration);
70+
}
71+
72+
[Test]
73+
public void ExceptionsInBeforeTransactionCompletionAreIgnored()
74+
{
75+
Assert.IsTrue(sessions.Settings.IsInterceptorsBeforeTransactionCompletionIgnoreExceptions);
76+
77+
C c = new C();
78+
c.ID = 1;
79+
c.Value = "value";
80+
81+
var sessionInterceptor = new SessionInterceptorThatThrowsExceptionAtBeforeTransactionCompletion();
82+
using (ISession s = sessions.OpenSession(sessionInterceptor))
83+
using (ITransaction t = s.BeginTransaction())
84+
{
85+
s.Save(c);
86+
try
87+
{
88+
t.Commit();
89+
}
90+
catch (BadException)
91+
{
92+
Assert.Fail("BadException not expected");
93+
}
94+
}
95+
96+
using (ISession s = sessions.OpenSession())
97+
{
98+
var objectInDb = s.Get<C>(1);
99+
100+
Assert.IsNotNull(objectInDb);
101+
102+
s.Delete(objectInDb);
103+
s.Flush();
104+
}
105+
}
106+
}
44107
}

src/NHibernate.Test/NHSpecificTest/NH1082/SessionInterceptorThatThrowsExceptionAtBeforeTransactionCompletion.cs

-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ namespace NHibernate.Test.NHSpecificTest.NH1082
44
{
55
public class SessionInterceptorThatThrowsExceptionAtBeforeTransactionCompletion : EmptyInterceptor
66
{
7-
public bool SessionWasRollbacked { get; set; }
8-
97
public override void BeforeTransactionCompletion(ITransaction tx)
108
{
119
throw new BadException();

src/NHibernate/Cfg/Environment.cs

+7
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,13 @@ public static string Version
171171
/// <summary> Enable ordering of insert statements for the purpose of more effecient batching.</summary>
172172
public const string OrderInserts = "order_inserts";
173173

174+
/// <summary>
175+
/// If this setting is set to false, exceptions in IInterceptor.BeforeTransactionCompletion bubble to the caller of ITransaction.Commit and abort the commit.
176+
/// If this setting is set to true, exceptions in IInterceptor.BeforeTransactionCompletion are ignored and the commit is performed.
177+
/// The default setting is false.
178+
/// </summary>
179+
public const string InterceptorsBeforeTransactionCompletionIgnoreExceptions = "interceptors.beforetransactioncompletion_ignore_exceptions";
180+
174181
private static readonly Dictionary<string, string> GlobalProperties;
175182

176183
private static IBytecodeProvider BytecodeProviderInstance;

src/NHibernate/Cfg/Settings.cs

+131-129
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,132 @@
1-
using System.Collections.Generic;
2-
using System.Data;
3-
using NHibernate.AdoNet;
4-
using NHibernate.AdoNet.Util;
5-
using NHibernate.Cache;
6-
using NHibernate.Connection;
7-
using NHibernate.Exceptions;
8-
using NHibernate.Hql;
9-
using NHibernate.Linq.Functions;
10-
using NHibernate.Transaction;
11-
12-
namespace NHibernate.Cfg
13-
{
14-
/// <summary>
15-
/// Settings that affect the behavior of NHibernate at runtime.
16-
/// </summary>
17-
public sealed class Settings
18-
{
19-
public Settings()
20-
{
21-
MaximumFetchDepth = -1;
22-
}
23-
24-
// not ported - private TransactionManagerLookup transactionManagerLookup;
25-
// not ported - private bool strictJPAQLCompliance;
26-
27-
#region JDBC Specific (Not Ported)
28-
29-
//private int jdbcFetchSize;
30-
//private bool isJdbcBatchVersionedData;
31-
32-
#endregion
33-
public SqlStatementLogger SqlStatementLogger { get; internal set; }
34-
35-
public int MaximumFetchDepth { get; internal set; }
36-
37-
public IDictionary<string, string> QuerySubstitutions { get; internal set; }
38-
39-
public Dialect.Dialect Dialect { get; internal set; }
40-
41-
public int AdoBatchSize { get; internal set; }
42-
43-
public int DefaultBatchFetchSize { get; internal set; }
44-
45-
public bool IsScrollableResultSetsEnabled { get; internal set; }
46-
47-
public bool IsGetGeneratedKeysEnabled { get; internal set; }
48-
49-
public string DefaultSchemaName { get; set; }
50-
51-
public string DefaultCatalogName { get; internal set; }
52-
53-
public string SessionFactoryName { get; internal set; }
54-
55-
public bool IsAutoCreateSchema { get; internal set; }
56-
57-
public bool IsAutoDropSchema { get; internal set; }
58-
59-
public bool IsAutoUpdateSchema { get; internal set; }
60-
61-
public bool IsAutoValidateSchema { get; internal set; }
62-
63-
public bool IsAutoQuoteEnabled { get; internal set; }
64-
65-
public bool IsKeywordsImportEnabled { get; internal set; }
66-
67-
public bool IsQueryCacheEnabled { get; internal set; }
68-
69-
public bool IsStructuredCacheEntriesEnabled { get; internal set; }
70-
71-
public bool IsSecondLevelCacheEnabled { get; internal set; }
72-
73-
public string CacheRegionPrefix { get; internal set; }
74-
75-
public bool IsMinimalPutsEnabled { get; internal set; }
76-
77-
public bool IsCommentsEnabled { get; internal set; }
78-
79-
public bool IsStatisticsEnabled { get; internal set; }
80-
81-
public bool IsIdentifierRollbackEnabled { get; internal set; }
82-
83-
public bool IsFlushBeforeCompletionEnabled { get; internal set; }
84-
85-
public bool IsAutoCloseSessionEnabled { get; internal set; }
86-
87-
public ConnectionReleaseMode ConnectionReleaseMode { get; internal set; }
88-
89-
public ICacheProvider CacheProvider { get; internal set; }
90-
91-
public IQueryCacheFactory QueryCacheFactory { get; internal set; }
92-
93-
public IConnectionProvider ConnectionProvider { get; internal set; }
94-
95-
public ITransactionFactory TransactionFactory { get; internal set; }
96-
97-
public IBatcherFactory BatcherFactory { get; internal set; }
98-
99-
public IQueryTranslatorFactory QueryTranslatorFactory { get; internal set; }
100-
101-
public ISQLExceptionConverter SqlExceptionConverter { get; internal set; }
102-
103-
public bool IsWrapResultSetsEnabled { get; internal set; }
104-
105-
public bool IsOrderUpdatesEnabled { get; internal set; }
106-
107-
public bool IsOrderInsertsEnabled { get; internal set; }
108-
109-
public EntityMode DefaultEntityMode { get; internal set; }
110-
111-
public bool IsDataDefinitionImplicitCommit { get; internal set; }
112-
113-
public bool IsDataDefinitionInTransactionSupported { get; internal set; }
114-
115-
public bool IsNamedQueryStartupCheckingEnabled { get; internal set; }
116-
117-
#region NH specific
118-
119-
public IsolationLevel IsolationLevel { get; internal set; }
120-
121-
public bool IsOuterJoinFetchEnabled { get; internal set; }
122-
123-
/// <summary>
124-
/// Get the registry to provide Hql-Generators for known properties/methods.
125-
/// </summary>
126-
public ILinqToHqlGeneratorsRegistry LinqToHqlGeneratorsRegistry { get; internal set; }
127-
128-
#endregion
129-
}
1+
using System.Collections.Generic;
2+
using System.Data;
3+
using NHibernate.AdoNet;
4+
using NHibernate.AdoNet.Util;
5+
using NHibernate.Cache;
6+
using NHibernate.Connection;
7+
using NHibernate.Exceptions;
8+
using NHibernate.Hql;
9+
using NHibernate.Linq.Functions;
10+
using NHibernate.Transaction;
11+
12+
namespace NHibernate.Cfg
13+
{
14+
/// <summary>
15+
/// Settings that affect the behavior of NHibernate at runtime.
16+
/// </summary>
17+
public sealed class Settings
18+
{
19+
public Settings()
20+
{
21+
MaximumFetchDepth = -1;
22+
}
23+
24+
// not ported - private TransactionManagerLookup transactionManagerLookup;
25+
// not ported - private bool strictJPAQLCompliance;
26+
27+
#region JDBC Specific (Not Ported)
28+
29+
//private int jdbcFetchSize;
30+
//private bool isJdbcBatchVersionedData;
31+
32+
#endregion
33+
public SqlStatementLogger SqlStatementLogger { get; internal set; }
34+
35+
public int MaximumFetchDepth { get; internal set; }
36+
37+
public IDictionary<string, string> QuerySubstitutions { get; internal set; }
38+
39+
public Dialect.Dialect Dialect { get; internal set; }
40+
41+
public int AdoBatchSize { get; internal set; }
42+
43+
public int DefaultBatchFetchSize { get; internal set; }
44+
45+
public bool IsScrollableResultSetsEnabled { get; internal set; }
46+
47+
public bool IsGetGeneratedKeysEnabled { get; internal set; }
48+
49+
public string DefaultSchemaName { get; set; }
50+
51+
public string DefaultCatalogName { get; internal set; }
52+
53+
public string SessionFactoryName { get; internal set; }
54+
55+
public bool IsAutoCreateSchema { get; internal set; }
56+
57+
public bool IsAutoDropSchema { get; internal set; }
58+
59+
public bool IsAutoUpdateSchema { get; internal set; }
60+
61+
public bool IsAutoValidateSchema { get; internal set; }
62+
63+
public bool IsAutoQuoteEnabled { get; internal set; }
64+
65+
public bool IsKeywordsImportEnabled { get; internal set; }
66+
67+
public bool IsQueryCacheEnabled { get; internal set; }
68+
69+
public bool IsStructuredCacheEntriesEnabled { get; internal set; }
70+
71+
public bool IsSecondLevelCacheEnabled { get; internal set; }
72+
73+
public string CacheRegionPrefix { get; internal set; }
74+
75+
public bool IsMinimalPutsEnabled { get; internal set; }
76+
77+
public bool IsCommentsEnabled { get; internal set; }
78+
79+
public bool IsStatisticsEnabled { get; internal set; }
80+
81+
public bool IsIdentifierRollbackEnabled { get; internal set; }
82+
83+
public bool IsFlushBeforeCompletionEnabled { get; internal set; }
84+
85+
public bool IsAutoCloseSessionEnabled { get; internal set; }
86+
87+
public ConnectionReleaseMode ConnectionReleaseMode { get; internal set; }
88+
89+
public ICacheProvider CacheProvider { get; internal set; }
90+
91+
public IQueryCacheFactory QueryCacheFactory { get; internal set; }
92+
93+
public IConnectionProvider ConnectionProvider { get; internal set; }
94+
95+
public ITransactionFactory TransactionFactory { get; internal set; }
96+
97+
public IBatcherFactory BatcherFactory { get; internal set; }
98+
99+
public IQueryTranslatorFactory QueryTranslatorFactory { get; internal set; }
100+
101+
public ISQLExceptionConverter SqlExceptionConverter { get; internal set; }
102+
103+
public bool IsWrapResultSetsEnabled { get; internal set; }
104+
105+
public bool IsOrderUpdatesEnabled { get; internal set; }
106+
107+
public bool IsOrderInsertsEnabled { get; internal set; }
108+
109+
public EntityMode DefaultEntityMode { get; internal set; }
110+
111+
public bool IsDataDefinitionImplicitCommit { get; internal set; }
112+
113+
public bool IsDataDefinitionInTransactionSupported { get; internal set; }
114+
115+
public bool IsNamedQueryStartupCheckingEnabled { get; internal set; }
116+
117+
#region NH specific
118+
119+
public IsolationLevel IsolationLevel { get; internal set; }
120+
121+
public bool IsOuterJoinFetchEnabled { get; internal set; }
122+
123+
/// <summary>
124+
/// Get the registry to provide Hql-Generators for known properties/methods.
125+
/// </summary>
126+
public ILinqToHqlGeneratorsRegistry LinqToHqlGeneratorsRegistry { get; internal set; }
127+
128+
public bool IsInterceptorsBeforeTransactionCompletionIgnoreExceptions { get; internal set; }
129+
130+
#endregion
131+
}
130132
}

0 commit comments

Comments
 (0)