1

I have the following code:

Color color = new Color(null,"B12",null,1);

Session session=sessionFactory.openSession();
session.setFlushMode(FlushMode.MANUAL);

session.beginTransaction();
session.save(color);
session.clear();

session.getTransaction().commit();

session.close();

sessionFactory.close();

Documentation says about method Session#clear the following:

Evict all loaded instances and cancel all pending saves, updates and deletions.

Why doesn't it cancel save() and still stores data to database? Please help me with it!

acm
  • 2,086
  • 3
  • 16
  • 34
  • 1
    Why are you trying to do this? If you want a encapsulated action that can be cancelled, try using a transaction. While this appears to be a bug, I can't think of why it'd be done the way you've got it in production. – rcarver Feb 21 '17 at 20:05
  • i'm trying to learn things related to persistence context how this all works and don't understand why it does not work like it's supposed to. – Юра Арабчук Feb 21 '17 at 20:09
  • 1
    Is [this answer](http://stackoverflow.com/a/8118572/5078385) useful? – acm Feb 21 '17 at 20:11
  • What you have there is odd from a workflow perspective. Typically you'd rollback the transaction instead of committing it and not use clear at all. In my experience, the only time I end up using clear is to clear and flush during large numbers of insert/updates. As to why it still persists inside the transaction commit, it might be a bug, but its a corner case. – rcarver Feb 21 '17 at 20:21
  • I know that just curious about documentation. – Юра Арабчук Feb 21 '17 at 20:32

0 Answers0