org.apache.jackrabbit.core.persistence.db
Class DatabasePersistenceManager

java.lang.Object
  extended by org.apache.jackrabbit.core.persistence.AbstractPersistenceManager
      extended by org.apache.jackrabbit.core.persistence.db.DatabasePersistenceManager
All Implemented Interfaces:
PersistenceManager
Direct Known Subclasses:
JNDIDatabasePersistenceManager, SimpleDbPersistenceManager

public abstract class DatabasePersistenceManager
extends AbstractPersistenceManager

Abstract base class for database persistence managers. This class contains common functionality for database persistence manager subclasses that normally differ only in the way the database connection is acquired. Subclasses should override the getConnection() method to return the configured database connection.

See the SimpleDbPersistenceManager for a detailed description of the available configuration options and database behaviour.


Field Summary
protected  boolean autoReconnect
           
protected  String blobDeleteSQL
           
protected  FileSystem blobFS
          file system where BLOB data is stored (if externalBLOBs==true)
protected  String blobInsertSQL
           
protected  String blobSelectExistSQL
           
protected  String blobSelectSQL
           
protected  BLOBStore blobStore
          BLOBStore that manages BLOB data in the file system (if externalBLOBs==true)
protected  String blobUpdateSQL
           
protected  Connection con
           
protected  boolean externalBLOBs
           
protected static int INITIAL_BUFFER_SIZE
           
protected  boolean initialized
           
protected  String nodeReferenceDeleteSQL
           
protected  String nodeReferenceInsertSQL
           
protected  String nodeReferenceSelectExistSQL
           
protected  String nodeReferenceSelectSQL
           
protected  String nodeReferenceUpdateSQL
           
protected  String nodeStateDeleteSQL
           
protected  String nodeStateInsertSQL
           
protected  String nodeStateSelectExistSQL
           
protected  String nodeStateSelectSQL
           
protected  String nodeStateUpdateSQL
           
protected  String propertyStateDeleteSQL
           
protected  String propertyStateInsertSQL
           
protected  String propertyStateSelectExistSQL
           
protected  String propertyStateSelectSQL
           
protected  String propertyStateUpdateSQL
           
protected  String schema
           
protected static String SCHEMA_OBJECT_PREFIX_VARIABLE
           
protected  String schemaObjectPrefix
           
protected static int SLEEP_BEFORE_RECONNECT
           
 
Constructor Summary
DatabasePersistenceManager()
          Creates a new DatabasePersistenceManager instance.
 
Method Summary
protected  void buildSQLStatements()
          Builds the SQL statements
protected  void checkSchema()
          Checks if the required schema objects exist and creates them if they don't exist yet.
 void close()
          Closes the persistence manager.
protected  void closeConnection(Connection connection)
          Closes the given database connection.
protected  void closeResultSet(ResultSet rs)
           
protected  void closeStatement(Statement stmt)
           
protected  String createSchemaSql(String sql)
          Replace wildcards and return the expanded SQL statement.
 void destroy(NodeReferences refs)
          Destroy a node references object.
 void destroy(NodeState state)
          Destroy a node state.
 void destroy(PropertyState state)
          Destroy a property state.
protected  Statement executeStmt(String sql, Object[] params)
          Executes the given SQL statement with the specified parameters.
 boolean exists(NodeId id)
          Checks whether the identified node exists.
 boolean exists(PropertyId id)
          Checks whether the identified property exists.
 boolean existsReferencesTo(NodeId targetId)
          Checks whether references of the identified target node exist.
protected  Connection getConnection()
          Abstract factory method for creating a new database connection.
 String getSchema()
           
protected  InputStream getSchemaDDL()
          Returns an input stream to the schema DDL resource.
 String getSchemaObjectPrefix()
           
 void init(PMContext context)
          Initializes the persistence manager.
protected  void initConnection()
          Initializes the database connection used by this persistence manager.
protected  void initPreparedStatements()
          Initializes the map of prepared statements.
 boolean isExternalBLOBs()
           
 boolean isSchemaCheckEnabled()
           
 NodeState load(NodeId id)
          Load the persistent members of a node state.
 PropertyState load(PropertyId id)
          Load the persistent members of a property state.
 NodeReferences loadReferencesTo(NodeId targetId)
          Load the persisted references to the node with the given identifier.
protected  void logException(String message, SQLException se)
           
protected  void prepareSchemaObjectPrefix()
          Makes sure that schemaObjectPrefix does only consist of characters that are allowed in names on the target database.
protected  boolean reestablishConnection()
          Re-establishes the database connection.
protected  void resetStatement(PreparedStatement stmt)
          Resets the given PreparedStatement by clearing the parameters and warnings contained.
 void setExternalBLOBs(boolean externalBLOBs)
           
 void setExternalBLOBs(String externalBLOBs)
           
 void setSchema(String schema)
           
 void setSchemaCheckEnabled(boolean enabled)
           
 void setSchemaObjectPrefix(String schemaObjectPrefix)
           
 void store(ChangeLog changeLog)
          Right now, this iterates over all items in the changelog and calls the individual methods that handle single item states or node references objects.
 void store(NodeReferences refs)
          Store a references object.
 void store(NodeState state)
          Store a node state.
 void store(PropertyState state)
          Store a property state.
 
Methods inherited from class org.apache.jackrabbit.core.persistence.AbstractPersistenceManager
checkConsistency, createNew, createNew
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SCHEMA_OBJECT_PREFIX_VARIABLE

protected static final String SCHEMA_OBJECT_PREFIX_VARIABLE
See Also:
Constant Field Values

initialized

protected boolean initialized

schema

protected String schema

schemaObjectPrefix

protected String schemaObjectPrefix

externalBLOBs

protected boolean externalBLOBs

INITIAL_BUFFER_SIZE

protected static final int INITIAL_BUFFER_SIZE
See Also:
Constant Field Values

con

protected Connection con

autoReconnect

protected boolean autoReconnect

SLEEP_BEFORE_RECONNECT

protected static final int SLEEP_BEFORE_RECONNECT
See Also:
Constant Field Values

nodeStateInsertSQL

protected String nodeStateInsertSQL

nodeStateUpdateSQL

protected String nodeStateUpdateSQL

nodeStateSelectSQL

protected String nodeStateSelectSQL

nodeStateSelectExistSQL

protected String nodeStateSelectExistSQL

nodeStateDeleteSQL

protected String nodeStateDeleteSQL

propertyStateInsertSQL

protected String propertyStateInsertSQL

propertyStateUpdateSQL

protected String propertyStateUpdateSQL

propertyStateSelectSQL

protected String propertyStateSelectSQL

propertyStateSelectExistSQL

protected String propertyStateSelectExistSQL

propertyStateDeleteSQL

protected String propertyStateDeleteSQL

nodeReferenceInsertSQL

protected String nodeReferenceInsertSQL

nodeReferenceUpdateSQL

protected String nodeReferenceUpdateSQL

nodeReferenceSelectSQL

protected String nodeReferenceSelectSQL

nodeReferenceSelectExistSQL

protected String nodeReferenceSelectExistSQL

nodeReferenceDeleteSQL

protected String nodeReferenceDeleteSQL

blobInsertSQL

protected String blobInsertSQL

blobUpdateSQL

protected String blobUpdateSQL

blobSelectSQL

protected String blobSelectSQL

blobSelectExistSQL

protected String blobSelectExistSQL

blobDeleteSQL

protected String blobDeleteSQL

blobFS

protected FileSystem blobFS
file system where BLOB data is stored (if externalBLOBs==true)


blobStore

protected BLOBStore blobStore
BLOBStore that manages BLOB data in the file system (if externalBLOBs==true)

Constructor Detail

DatabasePersistenceManager

public DatabasePersistenceManager()
Creates a new DatabasePersistenceManager instance.

Method Detail

getSchemaObjectPrefix

public String getSchemaObjectPrefix()

setSchemaObjectPrefix

public void setSchemaObjectPrefix(String schemaObjectPrefix)

getSchema

public String getSchema()

setSchema

public void setSchema(String schema)

isExternalBLOBs

public boolean isExternalBLOBs()

setExternalBLOBs

public void setExternalBLOBs(boolean externalBLOBs)

setExternalBLOBs

public void setExternalBLOBs(String externalBLOBs)

isSchemaCheckEnabled

public final boolean isSchemaCheckEnabled()
Returns:
whether the schema check is enabled

setSchemaCheckEnabled

public final void setSchemaCheckEnabled(boolean enabled)
Parameters:
enabled - set whether the schema check is enabled

init

public void init(PMContext context)
          throws Exception
Initializes the persistence manager. The persistence manager is permanently bound to the given context, and any required external resources are acquired.

An appropriate exception is thrown if the persistence manager initialization fails for whatever reason. In this case the state of the persistence manager is undefined and the instance should be discarded.

Parameters:
context - persistence manager context
Throws:
Exception - if the persistence manager initialization failed

close

public void close()
           throws Exception
Closes the persistence manager. The consistency of the persistent storage is guaranteed and all acquired resources are released. It is an error to invoke any methods on a closed persistence manager, and implementations are free to enforce this constraint by throwing IllegalStateExceptions in such cases.

An appropriate exception is thrown if the persistence manager could not be closed properly. In this case the state of the persistence manager is undefined and the instance should be discarded.

Throws:
Exception - if the persistence manager failed to close properly

store

public void store(ChangeLog changeLog)
           throws ItemStateException
Right now, this iterates over all items in the changelog and calls the individual methods that handle single item states or node references objects. Properly implemented, this method should ensure that changes are either written completely to the underlying persistence layer, or not at all. Atomically saves the given set of changes.

Specified by:
store in interface PersistenceManager
Overrides:
store in class AbstractPersistenceManager
Parameters:
changeLog - change log containing states that were changed
Throws:
ItemStateException - if the changes could not be saved

load

public NodeState load(NodeId id)
               throws NoSuchItemStateException,
                      ItemStateException
Load the persistent members of a node state.

Parameters:
id - node id
Returns:
loaded node state
Throws:
NoSuchItemStateException - if the node state does not exist
ItemStateException - if another error occurs

load

public PropertyState load(PropertyId id)
                   throws NoSuchItemStateException,
                          ItemStateException
Load the persistent members of a property state.

Parameters:
id - property id
Returns:
loaded property state
Throws:
NoSuchItemStateException - if the property state does not exist
ItemStateException - if another error occurs

store

public void store(NodeState state)
           throws ItemStateException
Store a node state. Subclass responsibility.

This method uses shared PreparedStatements which must be executed strictly sequentially. Because this method synchronizes on the persistence manager instance there is no need to synchronize on the shared statement. If the method would not be sychronized the shared statements would have to be synchronized.

Specified by:
store in class AbstractPersistenceManager
Parameters:
state - node state to store
Throws:
ItemStateException - if an error occurs

store

public void store(PropertyState state)
           throws ItemStateException
Store a property state. Subclass responsibility.

This method uses shared PreparedStatements which must be executed strictly sequentially. Because this method synchronizes on the persistence manager instance there is no need to synchronize on the shared statement. If the method would not be sychronized the shared statements would have to be synchronized.

Specified by:
store in class AbstractPersistenceManager
Parameters:
state - property state to store
Throws:
ItemStateException - if an error occurs

destroy

public void destroy(NodeState state)
             throws ItemStateException
Destroy a node state. Subclass responsibility.

Specified by:
destroy in class AbstractPersistenceManager
Parameters:
state - node state to destroy
Throws:
ItemStateException - if an error occurs

destroy

public void destroy(PropertyState state)
             throws ItemStateException
Destroy a property state. Subclass responsibility.

Specified by:
destroy in class AbstractPersistenceManager
Parameters:
state - property state to destroy
Throws:
ItemStateException - if an error occurs

loadReferencesTo

public NodeReferences loadReferencesTo(NodeId targetId)
                                throws NoSuchItemStateException,
                                       ItemStateException
Load the persisted references to the node with the given identifier.

Parameters:
targetId - reference target node id
Throws:
NoSuchItemStateException - if the target node does not exist
ItemStateException - if another error occurs

store

public void store(NodeReferences refs)
           throws ItemStateException
Store a references object. Subclass responsibility.

This method uses shared PreparedStatements which must be executed strictly sequentially. Because this method synchronizes on the persistence manager instance there is no need to synchronize on the shared statement. If the method would not be sychronized the shared statements would have to be synchronized.

Specified by:
store in class AbstractPersistenceManager
Parameters:
refs - references object to store
Throws:
ItemStateException - if an error occurs

destroy

public void destroy(NodeReferences refs)
             throws ItemStateException
Destroy a node references object. Subclass responsibility.

Specified by:
destroy in class AbstractPersistenceManager
Parameters:
refs - node references object to destroy
Throws:
ItemStateException - if an error occurs

exists

public boolean exists(NodeId id)
               throws ItemStateException
Checks whether the identified node exists.

Parameters:
id - node id
Returns:
true if the node exists, false otherwise
Throws:
ItemStateException - on persistence manager errors

exists

public boolean exists(PropertyId id)
               throws ItemStateException
Checks whether the identified property exists.

Parameters:
id - property id
Returns:
true if the property exists, false otherwise
Throws:
ItemStateException - on persistence manager errors

existsReferencesTo

public boolean existsReferencesTo(NodeId targetId)
                           throws ItemStateException
Checks whether references of the identified target node exist.

Parameters:
targetId - target node id
Returns:
true if the references exist, false otherwise
Throws:
ItemStateException - on persistence manager errors

initConnection

protected void initConnection()
                       throws Exception
Initializes the database connection used by this persistence manager.

Subclasses should normally override the getConnection() method instead of this one. The default implementation calls getConnection() to get the database connection and disables the autocommit feature.

Throws:
Exception - if an error occurs

getConnection

protected Connection getConnection()
                            throws Exception
Abstract factory method for creating a new database connection. This method is called by init(PMContext) when the persistence manager is started. The returned connection should come with the default JDBC settings, as the init(PMContext) method will explicitly set the autoCommit and other properties as needed.

Note that the returned database connection is kept during the entire lifetime of the persistence manager, after which it is closed by close() using the closeConnection(Connection) method.

Returns:
new connection
Throws:
Exception - if an error occurs

closeConnection

protected void closeConnection(Connection connection)
                        throws Exception
Closes the given database connection. This method is called by close() to close the connection acquired using getConnection() when the persistence manager was started.

The default implementation just calls the Connection.close() method of the given connection, but subclasses can override this method to provide more extensive database and connection cleanup.

Parameters:
connection - database connection
Throws:
Exception - if an error occurs

reestablishConnection

protected boolean reestablishConnection()
Re-establishes the database connection. This method is called by store(ChangeLog) and executeStmt(String, Object[]) after a SQLException had been encountered.

Returns:
true if the connection could be successfully re-established, false otherwise.

executeStmt

protected Statement executeStmt(String sql,
                                Object[] params)
                         throws SQLException
Executes the given SQL statement with the specified parameters. If a SQLException is encountered and autoReconnect==true one attempt is made to re-establish the database connection and re-execute the statement.

Parameters:
sql - statement to execute
params - parameters to set
Returns:
the Statement object that had been executed
Throws:
SQLException - if an error occurs

resetStatement

protected void resetStatement(PreparedStatement stmt)
Resets the given PreparedStatement by clearing the parameters and warnings contained.

NOTE: This method MUST be called in a synchronized context as neither this method nor the PreparedStatement instance on which it operates are thread safe.

Parameters:
stmt - The PreparedStatement to reset. If null this method does nothing.

closeResultSet

protected void closeResultSet(ResultSet rs)

closeStatement

protected void closeStatement(Statement stmt)

logException

protected void logException(String message,
                            SQLException se)

prepareSchemaObjectPrefix

protected void prepareSchemaObjectPrefix()
                                  throws Exception
Makes sure that schemaObjectPrefix does only consist of characters that are allowed in names on the target database. Illegal characters will be escaped as necessary.

Throws:
Exception - if an error occurs

checkSchema

protected void checkSchema()
                    throws Exception
Checks if the required schema objects exist and creates them if they don't exist yet.

Throws:
Exception - if an error occurs

createSchemaSql

protected String createSchemaSql(String sql)
Replace wildcards and return the expanded SQL statement.

Parameters:
sql - The SQL with embedded wildcards.
Returns:
The SQL with no wildcards present.

getSchemaDDL

protected InputStream getSchemaDDL()
Returns an input stream to the schema DDL resource.

Returns:
an input stream to the schema DDL resource.

buildSQLStatements

protected void buildSQLStatements()
Builds the SQL statements


initPreparedStatements

protected void initPreparedStatements()
                               throws SQLException
Initializes the map of prepared statements.

Throws:
SQLException - if an error occurs


Copyright © 2004-2010 The Apache Software Foundation. All Rights Reserved.