user3239652
user3239652

Reputation: 785

Flushing not saving data in a database

When I flush stuff externally, it doesn't save it in a database. For example the code below doesn't save it in the DB:

public class save {
    public static void main(String[] args) {
        emp a=new emp("klds","asda",12231);
        emp b=new emp("asd","asd",213);
        emp c=new emp("ERTE", "retre", 1323434);
        Session session=myfactory.getSession();
        session.save(a);
        session.save(b);
        session.save(c);
        session.flush();
        session.clear();

    }

But when I use transactions, it does save it in a DB. Why does it do this?

Upvotes: 0

Views: 1867

Answers (2)

Karibasappa G C
Karibasappa G C

Reputation: 2732

The flush is just synchronization with the db and then you need to commit to see the changes.

And if you do just commit .It automatically flushes and you can see your changes from another session too.

when we call session.flush() our statements will execute in database but will not be committed.

but if we dont call flush() method on session object and if we directly call commit method....commit will internally call flush(do the work of executing statements on the database) and then committing.

so commit=flush+commit

so when we call method flush() on Session object then it will not commit but hits the database and executes the query and rollback the same

so to commit we must use commit() on Transaction object

Upvotes: 2

Rafik BELDI
Rafik BELDI

Reputation: 4158

Try changing the FlushMode of the session to MANUAL:

session.setFlushMode(FlushMode.MANUAL)

Upvotes: 0

Related Questions