You are here: Basics Operations & Concepts > ACID Properties And Transactions > db4o Transactions

db4o Transactions

All db4o operations are transactional and there's always a transaction running. Each object container has its own transaction running. The transaction is started implicitly.

You can commit the transaction at any time. When the commit-call returns, all changes are made persistent.

Commit A Transactions

In order to commit a transaction, you need to call the commit-method. This will make all changes of the current transaction persistent. When the commit call is finished, everything is safely stored. If something goes wrong during the commit-operation or the commit-operation is interrupted (power-off, crash etc) the database has the state of either before or after the commit-call.

container.Store(new Pilot("John"));
container.Store(new Pilot("Joanna"));

container.Commit();
Transactions.cs: Commit changes
container.Store(New Pilot("John"))
container.Store(New Pilot("Joanna"))

container.Commit()
Transactions.vb: Commit changes

Rollback A Transaction

Of course you also can rollback a transaction. Just call rollback on the object container.

container.Store(new Pilot("John"));
container.Store(new Pilot("Joanna"));

container.Rollback();
Transactions.cs: Rollback changes
container.Store(New Pilot("John"))
container.Store(New Pilot("Joanna"))

container.Rollback()
Transactions.vb: Rollback changes

Note that when you rollback the changes, db4o won't rollback the objects in memory. All objects in memory will keep the state. If you want to make sure that objects in memory have the same state as in the database, you need to refresh the objects.

Pilot pilot = container.Query<Pilot>()[0];
pilot.Name = "New Name";
container.Store(pilot);
container.Rollback();

// use refresh to return the in memory objects back
// to the state in the database.
container.Ext().Refresh(pilot, int.MaxValue);
Transactions.cs: Refresh objects after rollback
Dim pilot As Pilot = container.Query(Of Pilot)()(0)
pilot.Name = "New Name"
container.Store(pilot)
container.Rollback()

' use refresh to return the in memory objects back
' to the state in the database.
container.Ext().Refresh(pilot, Integer.MaxValue)
Transactions.vb: Refresh objects after rollback

Implicit Commits

db4o commits implicitly when you close the object-container. The assumption is that normally you want to make the changes persistent when you close the object container. That's why it commits automatically. When you want to prevent this you should rollback the transaction before closing the container,

Multiple Concurrent Transactions

db4o transactions are always bound to their object container. When you want multiple concurrent transactions, you need to open multiple object containers. You can easily do this with the open session method. See "Session Containers"

Note that in this mode, db4o uses the read committed isolation. See "Isolation"

using (IObjectContainer rootContainer = Db4oEmbedded.OpenFile(DatabaseFileName))
{
    // open the db4o-session. For example at the beginning for a web-request
    using (IObjectContainer session = rootContainer.Ext().OpenSession())
    {
        // do the operations on the session-container
        session.Store(new Person("Joe"));
    }
}
Db4oSessions.cs: Session object container
Using rootContainer As IObjectContainer = Db4oEmbedded.OpenFile(DatabaseFileName)
    ' open the db4o-session. For example at the beginning for a web-request
    Using session As IObjectContainer = rootContainer.Ext().OpenSession()
        ' do the operations on the session-container
        session.Store(New Person("Joe"))
    End Using
End Using
Db4oSessions.vb: Session object container