Skip to content

Make ISession.IsDirty() method immutable operation #1413

Open
@bahusoid

Description

@bahusoid

ISession.IsDirty() method description:

Does this ISession contain any changes which must be synchronized with the database? Would any SQL be executed if we flushed this session?

So expected behavior: method should not persist any entities (work in read-only mode)

And current behavior: It's very unexpected that this call can lead to data modifications in DB:
all cascaded entities are saved (at least all with Identity ID lead to DB inserts - very unexpected and dangerous behavior)
Edit: that point is fixed in 5.4 by #2752, but the session state can still be mutated by IsDirty: this needs still to be fixed.

My use case for using this ISession.IsDirty() is the following:

var entity = LoadEntity();
//Do some modifications to entity like entity.Children.Add(child); 
//which is mapped with Cascade.All; Also child Id is Identity generated by DB
entity.Children.Add(CreateNewChild());

//... And at the end of the request:
if(session.IsDirty())
{
	//do some resource consuming stuff like open DTC transaction or something else that needs to be done only if database modifications are requried
	using(var dtcTransaction = OpenDTCTransaction())
	{
		session.Flush();
		//...do some stuff with legacy VB objects
	}
}

With current implementation I can't use IsDirty() as it will save child entity.Children elements without transaction context - very dangerous and unexpected.
See test case #1414

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions