Reputation: 443
I'm working with c# and nhibernate to access the database. I have two or three terminals who can realize same actions, like edit, or remove entities from the same DB. My problem is that my terminal does not realize if another terminal made any change on the database, although i refresh the data executing the queries again. I think it's due to the cache. I've tryed Flush() and Evict() commands with no results. I wish somebody could help me, sorry for my terrible english
-----Info Added -------
It's a c# desktop application. I don't see any errors, the problem is that when i edit or delete i need to know if the entity was modified from another terminal. and when i bring the entity from database, i'm not seeig the changes mades from another terminal until y restart the application.
The session is created by the next code :
[PossibleLongOperation]
private void DoConnectionWork(string nhibernateConfigPath)
{
String appPath = Directory.GetParent(Assembly.GetAssembly(GetType()).Location).FullName;
String serializablefilePath = Path.Combine(appPath, "configuration.serialized");
try
{
if (IsConfigurationFileValid(serializablefilePath))
{
Configuration = LoadConfigurationFromFile(serializablefilePath);
}
else
{
// configuration is immutable, store last returned value
Configuration = new Configuration();
Configuration.Configure(nhibernateConfigPath);
Configuration.AddAssembly(typeof(ObjectEntity).Assembly);
SaveConfigurationToFile(serializablefilePath, Configuration);
new SchemaUpdate(Configuration).Execute(true, true);
Console.WriteLine(@"Se solicitó la actualización de datos");
}
//NHibernateSchemaExport();
}
catch (Exception ex)
{
//if(File.Exists(serializablefilePath))
// File.Delete(serializablefilePath);
throw new GenericRepositoryException(string.Format("Error while configuring NHibernate: {0}.", ex.Message), ex);
}
try
{
SessionFactory = Configuration.BuildSessionFactory();
}
catch (Exception ex)
{
//if (File.Exists(serializablefilePath))
// File.Delete(serializablefilePath);
throw new GenericRepositoryException(string.Format("Error while building NH session factory: {0}.", ex.Message), ex);
}
}
----------Added-------------
this is the exception i get after Clear() the session
09-02-2012 13:22:54|Error|Error:
NHibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 394, of entity: Model.Pedido
en NHibernate.Engine.StatefulPersistenceContext.CheckUniqueness(EntityKey key, Object obj) en d:\CSharp\NH\NH\nhibernate\src\NHibernate\Engine\StatefulPersistenceContext.cs:línea 688
en NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.PerformUpdate(SaveOrUpdateEvent event, Object entity, IEntityPersister persister) en d:\CSharp\NH\NH\nhibernate\src\NHibernate\Event\Default\DefaultSaveOrUpdateEventListener.cs:línea 227
en NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsDetached(SaveOrUpdateEvent event) en d:\CSharp\NH\NH\nhibernate\src\NHibernate\Event\Default\DefaultSaveOrUpdateEventListener.cs:línea 186
en NHibernate.Event.Default.DefaultUpdateEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent event) en d:\CSharp\NH\NH\nhibernate\src\NHibernate\Event\Default\DefaultUpdateEventListener.cs:línea 29
en NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event) en d:\CSharp\NH\NH\nhibernate\src\NHibernate\Event\Default\DefaultSaveOrUpdateEventListener.cs:línea 53
en NHibernate.Impl.SessionImpl.FireUpdate(SaveOrUpdateEvent event) en d:\CSharp\NH\NH\nhibernate\src\NHibernate\Impl\SessionImpl.cs:línea 2666
en NHibernate.Impl.SessionImpl.Update(Object obj) en d:\CSharp\NH\NH\nhibernate\src\NHibernate\Impl\SessionImpl.cs:línea 524
en Besnik.GenericRepository.NHibernate.NHibernateUnitOfWork.Update[TEntity](TEntity entity)
en Besnik.GenericRepository.GenericRepository`2.Update(TEntity entity)
en Services.Services.Implementation.PedidoServiceImpl.<>c__DisplayClass8.<Anular>b__7() en C:\Repositorio\ProyectosCasinoClub\SPMontaPlatosOK\Code\Services\Services\Implementation\PedidoServiceImpl.cs:línea 72
en Services.Transaction.Transaction.Execute(Action transactionalAction, Action`1 onException) en C:\Repositorio\ProyectosCasinoClub\SPMontaPlatosOK\Code\Services\Transaction\Transaction.cs:línea 32
Upvotes: 2
Views: 2250
Reputation: 8201
Did you try clearing the L1 session cache, by session.Clear() ?
Upvotes: 3