Regarding the Castle Automatic Transaction Management Facility; I'm having some difficulties getting operations to actually save to the database without flushing the session.
I'm using the following components * NHibernate.dll v3.1.0.4000 * Castle.Core.dll v2.5.2.0 * Castle.Windsor.dll v2.5.3.0 * Castle.Facilities.NHibernateIntegration.dll v1.1.0.0 * Castle.Services.Transaction.dll v2.5.0.0 * Castle.Facilities.AutoTx.dll v2.5.1.0
I have followed the Castle documentation very closely and have not been able to resolve my issue.
My (web-)application follows the MVP pattern. The key parts of the (transactional) presenter-service are shown below:
<Transactional()> _
Public Class CampusEditPresenter
Inherits BasePresenter(Of ICampusEditView)
Public Sub New(ByVal view As ICampusEditView)
MyBase.New(view)
End Sub
...
<Transaction(TransactionMode.Requires)> _
Public Overridable Sub Save() Implements ICampusEditPresenter.Save
' Simplified
Using session As ISession = _sessionManager.OpenSession()
Dim campus As New Campus()
campus.Code = _view.Code
campus.ShortDescription = _view.ShortDescription
campus.LongDescription = _view.LongDescription
campus.StartDate = _view.StartDate
campus.EndDate = _view.EndDate
session.Save(campus)
End Using
End Sub
End Class
This presenter-service is registered in an installer:
container.Register( _
Component.For(Of CampusEditPresenter) _
.Interceptors(Of DebugLoggingInterceptor) _
.LifeStyle.Transient)
and resolved by the view (in the base):
Public Class BasePage(Of TPresenter)
Inherits Page
Protected _presenter As TPresenter
...
Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Init
_presenter = _container.Resolve(Of TPresenter)(New With {Key .view = Me})
End Sub
...
End Class
Public Class CampusEdit
Inherits BasePage(Of CampusEditPresenter)
Implements ICampusEditView
...
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
_presenter.Save()
End Sub
...
End Class
I have registered the NHibernate and Transaction facilities in an XML configuration file as follows:
<facility id="transaction" type="Castle.Facilities.AutoTx.TransactionFacility, Castle.Facilities.AutoTx" />
<facility id="nhibernate" type="Castle.Facilities.NHibernateIntegration.NHibernateFacility, Castle.Facilities.NHibernateIntegration" isWeb="true" configurationBuilder="[removed].AutoConfigurationBuilder, [removed]">
<factory id="nhibernate.factory">
<settings>
<item key="connection.driver_class">NHibernate.Driver.OracleClientDriver, NHibernate</item>
<item key="connection.connection_string">[removed]</item>
<item key="show_sql">false</item>
<item key="dialect">NHibernate.Dialect.Oracle10gDialect, NHibernate</item>
<item key="query.substitutions">true 1, false 0, yes 'Y', no 'N'</item>
<item key="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle&l开发者_如何转开发t;/item>
<item key="current_session_context_class">web</item>
<item key="hbm2ddl.keywords">auto-quote</item>
</settings>
</factory>
</facility>
and I have registered the SessionWebModule Http module in my Web.config:
<httpModules>
<add name="NHibernateSessionWebModule" type="Castle.Facilities.NHibernateIntegration.Components.Web.SessionWebModule, Castle.Facilities.NHibernateIntegration"/>
...
</httpModules>
Any ideas as to why this may not be working?
I can get everything working when I a) instansiate my own transactions from the ISession instance and maually commit these transactions, or if I b) use the Automatic Transaction Management AOP-mechanism AND manuall flush the session instance (though I shouldn't have to manually do this).
I would have also thought that SessionWebModule IHttpModule (which follows the open-session-per-request pattern) would cause my entities to be persisted, but this doesnt seem to be happening...
So I worked out the TransactionInterceptor was not getting registerd on my components.
After downloading the Castle.Facilities.AutomaticTransactionManagement source from github and stepping through, I found my issue and managed to resolve it.
Basically the TransactionFacility adds a contributor, TransactionComponentInspector, to the ComponentModelBuilder, which allows for some additional configuration contribution whilst building the component. In the case of the TransactionComponentInspector, it looks for a "Transactional" class attribute on the component and if it exists it will register a TransactionInterceptor on the component. However, my components were never getting contributed to by the TransactionComponentInspector.
To configure/register my components on the container, I use Installers. I configure the container itself using XML, which references these installers as well as any facilities (e.g. NHibernate-integration/logging etc.). Anyways, I believe it may have been some kind of ordering issue whereby my components might have been getting registered before the transaction facility. As such components registered before the TransactionFacility were not getting contributed-to by the TransactionComponentInspector and were therefore not getting a TransactionInterceptor registered on the component. Once I realised this I manually configured the container (with the correct order of things) and everything seemed to work!!!
Now I've got to try and work out how to do this in my XML configuration. If I can't, I guess I'll dump this and go for fluent configuration of the container (e.g. in the global HttpApplication).
[edit] see below:
_container = New WindsorContainer()
' TransactionFacility must be registered before components.
_container.AddFacility(Of TransactionFacility)()
_container.Install(Configuration.FromXmlFile("Configs\services.xml"))
I was experiencing very similar behavior. It turned out that I was creating a Logging Aspect Interceptor. I had created a Default Interceptor Selector so that I could apply logging where needed. In doing this, I was messing up the TransactionalInterceptor. Once I removed the Default Interceptor Selector, the Transactions started working.
Try SwampyFox's suggestion, then also try the nuget of 3.0.x of Tx, AutoTx and NHibFac, if that doesn't solve your problems. New development (if it's a bug) is going into those. Tell is the result of trying Foxy's suggestion.
精彩评论