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.
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();
container.Store(New Pilot("John")) container.Store(New Pilot("Joanna")) container.Commit()
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();
container.Store(New Pilot("John")) container.Store(New Pilot("Joanna")) container.Rollback()
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);
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)
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,
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")); } }
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