Prevayler

What is Prevayler?

Prevayler is an open source object persistence library for Java. It is an implementation of the Prevalent System design pattern, in which business objects are kept live in memory and transactions are journaled for system recovery. Prevayler is the simplest and fastest way to provide ACID persistence for your "plain old Java objects".

Jolt Productivity Award
Version 2.6 was released on 2013-01-15
Prevayler Usage

To persist an instance of

class MyData implements Serializable{
	//...
}

create a Prevayler for it:

Prevayler<MyData> prevayler = PrevaylerFactory.createPrevayler(new MyData());

and ensure any changes to it are made via the local variable in Transactions:

class MyChange implements org.prevayler.Transaction<MyData>{
	public void executeOn(MyData myData, Date date){
		//make any changes via "myData" here
	}
}
//execute a "MyChange":
prevayler.execute(new MyChange());

You can pass arguments into a Transaction:

//execute a "MyChange"
prevayler.execute(new MyChange(new MyArgument()));

via one or more instance variables in your Transaction:

class MyChange implements Transaction<MyData>{
	
	MyArgument myArgument;
	
	public MyChange(MyArgument myArgument){
		this.myArgument = myArgument;
	}

	public void executeOn(MyData myData, Date date){
		//You can use "myArgument" here, but upon
		//recovery execution it'll be a freshly
		//deserialized copy, so it won't reference
		//anything in "myData"
	}
}

To perform a Transaction with a query, implement TransactionWithQuery and set the second type parameter to your desired return type:

class MyChangeAndQuery implements TransactionWithQuery<MyData, MyReturnType>{
	public MyReturnType executeAndQuery(MyData myData, Date date){
		MyReturnType queryResult;
		//...
		return queryResult;
	}
}
//perform a "MyChangeAndQuery" and get the result
MyReturnType queryResult = prevayler.execute(new MyChangeAndQuery());

You can also perform a straightforward Query (which won't write any transaction to disk) or a SureTransactionWithQuery (which doesn't throw Exception).

You may want to write a snapshot of your persistent object to disk from time to time, to shorten recovery time upon system restart:

prevayler.takeSnapshot();