You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-user@db.apache.org by "P. Douglas Reeder" <re...@osu.edu> on 2007/12/12 16:03:49 UTC

Forcing Data to Disk on JPA Commit

Sorry to ask such a basic question, but after all the manuals and the  
list archives for "cache" and googling for "derby write cache", I'm  
still stumped.

I've written a simple web application wherein users take a survey and  
the results are saved to a database, using the Persistence API. It  
runs in Sun's Java Application Server, using TopLink Essentials as  
the Persistence provider and Derby as the database, all running on a  
Fedora Core 3 system. The application runs and data does indeed get  
written to the Derby database, but not for a while, generally several  
transactions later. Obviously, the data is being cached, which is  
generally a good thing. However, when a power outage occurs, there's  
no time for the cache to be written to the database. Transactions  
that happened two weeks earlier were not written to the database, and  
we lost data, which seems silly under the circumstances. The server  
is very lightly loaded -- there's only a few transactions each week.

So, I need to make sure the cache is written to disk within a  
reasonable length of time. How can I either
1) force data to be written to disk after every transaction
or
2) force data to be written to disk within some fixed time?

I asked about this on the TopLink mailing list, and was told TopLink  
doesn't cache writes.

The only thing about write caching I found in the Derby manuals was:
SYSCS_UTIL.SYSCS_CHECKPOINT_DATABASE system procedure
but this must be called using the JDBC API:
CallableStatement cs = conn.prepareCall ("CALL  
SYSCS_UTIL.SYSCS_CHECKPOINT_DATABASE()");
cs.execute();
but I don't think I have access to the connection variable when using  
JPA.

(I did find out about the on-disk write cache, but that isn't enabled  
on the drive.)


Here's my persistence.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/ 
persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http:// 
java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="OMSI-EE5cPersistenceUnit" transaction- 
type="JTA">
<provider>oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvide 
r</provider>
<jta-data-source>jdbc/omsiDatasource</jta-data-source>
<properties>
<property name="toplink.ddl-generation" value="create-tables"/>
</properties>
</persistence-unit>
</persistence>




Re: Forcing Data to Disk on JPA Commit

Posted by Alan Burlison <Al...@sun.com>.
Knut Anders Hatlen wrote:

> This is true, but many systems have enabled the write cache on the hard
> drive. This means that although Derby and the OS think the data have
> been written to non-volatile storage, they may still reside in the
> volatile write cache on the disk and can be lost in a power outage.

Linux & Windows run with the write cache turned on by default.  Solaris 
runs with it turned off by default under UFS, ZFS *may* enable it, 
depending on if it is safe to do so or not.

http://weblogs.java.net/blog/davidvc/archive/2005/10/the_story_of_th.html

-- 
Alan Burlison
--

Re: Forcing Data to Disk on JPA Commit

Posted by Knut Anders Hatlen <Kn...@Sun.COM>.
Bryan Pendleton <bp...@amberpoint.com> writes:

>> later. Obviously, the data is being cached, which is generally a
>> good thing. However, when a power outage occurs, there's no time for
>> the cache to be written to the database. Transactions that happened
>> two weeks earlier were not written to the database
>
> When a Derby transaction is committed, although the data pages
> may not be immediately written to disk, the log records for that
> transaction are immediately written to disk, before the commit finishes.
>
> So if the power should fail, when Derby is later restarted, it will
> perform recovery, read the committed transaction data from the log,
> and re-do those transactions.

This is true, but many systems have enabled the write cache on the hard
drive. This means that although Derby and the OS think the data have
been written to non-volatile storage, they may still reside in the
volatile write cache on the disk and can be lost in a power outage.

Olav mentioned in one of his presentations that he had run some
experiments where he cut the power while the database was under heavy
load. When the write cache on the disk was enabled, Derby failed to
recover in 3 out of 10 runs, and recovered with lost transactions in 2
out of 10 runs (see
http://home.online.no/~olmsan/publications/pres/apachecon07eu/DerbyApacheCon07eu.pdf). With
the write cache disabled, the database was successfully recovered in 10
out of 10 runs.

I think most desktop systems have the disk write cache enabled by
default.

-- 
Knut Anders

Re: Forcing Data to Disk on JPA Commit

Posted by Bryan Pendleton <bp...@amberpoint.com>.
> later. Obviously, the data is being cached, which is generally a good 
> thing. However, when a power outage occurs, there's no time for the 
> cache to be written to the database. Transactions that happened two 
> weeks earlier were not written to the database

When a Derby transaction is committed, although the data pages
may not be immediately written to disk, the log records for that
transaction are immediately written to disk, before the commit finishes.

So if the power should fail, when Derby is later restarted, it will
perform recovery, read the committed transaction data from the log,
and re-do those transactions.

The checkpoint mechanism that you read about in the Derby manual
is simply a way to speed the recovery processing, by bounding the
work that recovery may need to do should a system crash occur. The
absence of a checkpoint does not affect the fundamental durability
of a commit.

If you are seeing changes which are lost following a system crash
and restart, my first guess would be that your application (or the
persistence layer in between your application and Derby) is failing
to commit the transactions of interest, but rather is keeping those
transactions open, causing them to be aborted, not redone, when the
post-crash recovery processing occurs.

thanks,

bryan