Properties:
- Can be used when object state needs to be saved between interactions.
Implementation:
- Originator class is the class of which state needs to be saved. It should be able to save its state into Memento objects and restore its state from Memento objects.
- Memento objects keep state of originator object.
- Caretaker is the client code which saves and restores state of originator using Memento objects.
Java Standard Library Implementations:
- javax.faces.component. StateHolder implementations
- java.sql.Connection
Example Usage:
try{
//assume connection is acquired successfully
conn.setAutoCommit(false);
Statement statement = conn.createStatement();
String DELETE_SQL = "delete from Person";
statement.executeUpdate(DELETE_SQL);
//set a Savepoint, we want to ensure that delete is done
Savepoint savepoint = conn.setSavepoint("deleteSavePoint");
//Execute an incorrect SQL statement which fails
String INSERT_SQL = "INSERT INTO Person VALUES ('firstName', 'lastName', 'duplicate_candidat_key', 45)";
statement.executeUpdate(INSERT_SQL);
// If everything goes fine commit the changes.
conn.commit();
}catch(SQLException se){
// insert failed, return to deleteSavePoint
conn.rollback(savepoint);
conn.commit();
}
Comments
Post a Comment