简体   繁体   中英

Two applications, one database, NHibernate

I am currently developing two separate applications, one Microsoft WPF application and a Windows Service application.

The Windows Service application is responsible for opening a mailbox on specific time intervals and if there is new mail, create some entries in a SQL Server database corresponding to these mails. This application uses NHibernate as an ORM against the database.

On the other hand, I have a Microsoft WPF application that lets its users interact with the mails that is created by the Windows Service application. This interaction involves both reading and writing to the same exact database as the Windows Service application is using. The Microsoft WPF application also uses NHibernate as an ORM against the database (the exact same project, repository/unit of work pattern).

The problem is that when both applications run at the same time (as is the intention and design), the data provided to the respective applications through NHibernate will not be the same. I assume this is because of caching functionality and that the Microsoft WPF application's NHibernate instance is unaware of the changes that the Windows Service application's NHibernate instance is making to the database (and vice versa).

Any suggestions on how to remedy this behavior? I looked into the IStatelessSession interface but I did not like it that much. Is there a way to force NHibernate to refresh its state only at a programmatically specified time?

Thanks for any response!

NHibernate will keep track of its objects during the lifetime of a session, unless you manually Evict() entities from the session. Depending on how your application works you could create new short-lived sessions to query the list of database entries and create another session that handles getting and updating individual items. Creating a session isn't too expensive (creating the session factory is, but you only need one) and you wouldn't be forced to work around NHibernate's session cache.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM