com.jenkov.mrpersister
Class PersistenceManager

java.lang.Object
  extended by com.jenkov.mrpersister.PersistenceManager

public class PersistenceManager
extends java.lang.Object

This class is the starting point of all Mr. Persister use. Create an instance of PersistenceManager, then ask for its factories, then create an IGenericDao instance and start reading and writing objects. Many things are cached inside the PersistenceManager, so you should reuse the same instance once you have created it. Assign it to a constant, or make it a singleton, like this:

public static final PERSISTENCE_MANAGER = new PersistenceManager();

IGenericDao dao = PERSISTENCE_MANAGER.getGenericDaoFactory().createDao(connection);


The PersistenceManager instance should be reused throughout the application lifetime. Each application should create it's own PersistenceManager instance.

It is safe to share PersistenceManager instances if:

1) The components sharing them are reading the same type of objects from the same database.
2) The components sharing them are reading different types of objects from the same or different databases.


When sharing PersistenceManager instance be aware of what persistence configurations are used by the components sharing the PersistenceManager instance.

Author:
Jakob Jenkov - Copyright 2005 Jenkov Development

Constructor Summary
PersistenceManager()
          Creates an empty PersistenceManager instance.
PersistenceManager(javax.sql.DataSource dataSource)
          Creates a PersistenceManager instance and stores the given DataSource in the default persistence configuration.
 
Method Summary
 IDaos createDaos()
          Equivalent to calling getDaosFactory().createDao();
 IDaos createDaos(java.sql.Connection connection)
          Equivalent to calling getDaosFactory().createDao(connection);
 java.lang.Object execute(IDaoCommand daoCommand)
          Equivalent to calling getDaoCommandExecutor().execute(daoCommand);
 java.lang.Object executeTransaction(IDaoCommand daoCommand)
          Equivalent to calling getDaoCommandExecutor().executeTransaction(daoCommand);
 java.lang.Object executeTransactionUnchecked(IDaoCommand daoCommand)
          Equivalent to calling getDaoCommandExecutorUnchecked().executeTransaction(daoCommand);
 java.lang.Object executeUnchecked(IDaoCommand daoCommand)
          Equivalent to calling getDaoCommandExecutorUnchecked().execute(daoCommand);
 IPersistenceConfigurationFactory getConfigurationFactory()
          Returns the persistence configuration factory currently in use.
 IDaoCommandExecutor getDaoCommandExecutor()
           
 IDaoCommandExecutorUnchecked getDaoCommandExecutorUnchecked()
           
 IDaosFactory getDaosFactory()
           
 IPersistenceConfiguration getDefaultConfiguration()
          Returns the default persistence configuration of this PersistenceManager.
 IGenericDaoFactory getGenericDaoFactory()
          Returns the generic dao factory currently used by Mr Persister.
 IObjectMappingFactory getObjectMappingFactory()
          Returns the object method factory set for the Mr Persister API.
 void setConfigurationFactory(IPersistenceConfigurationFactory factory)
          Sets the persistence configuration factory to be used with Mr.
 void setDaoCommandExecutor(IDaoCommandExecutor daoCommandExecutor)
           
 void setDaoCommandExecutorUnchecked(IDaoCommandExecutorUnchecked daoCommandExecutorUnchecked)
           
 void setDaosFactory(IDaosFactory daosFactory)
           
 void setDataSource(javax.sql.DataSource dataSource)
          Sets the default data source used by this PeristenceManager instance, and used by the default IPersistenceConfiguration instance too.
 void setDataSource(javax.sql.DataSource dataSource, java.lang.Object configKey)
          Sets the data source to be used by the IPersistenceConfiguration stored by configKey in the configuration factory of this PersistenceManager.
 void setGenericDaoFactory(IGenericDaoFactory factory)
          Sets the generic dao factory to be used with Mr Persister.
 void setObjectMappingFactory(IObjectMappingFactory factory)
          Sets the object method factory set for the Mr Persister API.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PersistenceManager

public PersistenceManager()
Creates an empty PersistenceManager instance.


PersistenceManager

public PersistenceManager(javax.sql.DataSource dataSource)
Creates a PersistenceManager instance and stores the given DataSource in the default persistence configuration.

Parameters:
dataSource -
Method Detail

getConfigurationFactory

public IPersistenceConfigurationFactory getConfigurationFactory()
Returns the persistence configuration factory currently in use. The persistence factory replaces the MrPersister.createConfiguration() methods. Note: You will hardly ever need to access the persistence configuration factory directly.

Returns:
The persistence factory currently in use.

setConfigurationFactory

public void setConfigurationFactory(IPersistenceConfigurationFactory factory)
Sets the persistence configuration factory to be used with Mr. Persister in the future. The persistence factory replaces the MrPersister.createConfiguration() methods. You should never have to change the persistence factory. Only do so if you know what you are doing. Note: You will hardly ever need to set the persistence configuration factory yourself.

Parameters:
factory - The persistence factory to use.

getDefaultConfiguration

public IPersistenceConfiguration getDefaultConfiguration()
Returns the default persistence configuration of this PersistenceManager. Most of the time you will only need the default persistence configuration. If you need more, access the persistence configuration factories to create them.

Returns:
default persistence configuration of this PersistenceManager.

getObjectMappingFactory

public IObjectMappingFactory getObjectMappingFactory()
Returns the object method factory set for the Mr Persister API. This is by default set the implementation that comes with Mr. Persister.

Returns:
The object method factory set for the Mr. Persister API.

setObjectMappingFactory

public void setObjectMappingFactory(IObjectMappingFactory factory)
Sets the object method factory set for the Mr Persister API. This is by default set the implementation that comes with Mr. Persister.

Parameters:
factory - The object method factory to be used with the Mr. Persister API.

getGenericDaoFactory

public IGenericDaoFactory getGenericDaoFactory()
Returns the generic dao factory currently used by Mr Persister.

Returns:
The generic dao factory currently used by Mr Persister.

setGenericDaoFactory

public void setGenericDaoFactory(IGenericDaoFactory factory)
Sets the generic dao factory to be used with Mr Persister. Most likely you will not need to change it.

Parameters:
factory - The generic dao factory to be used with Mr. Persister.

getDaosFactory

public IDaosFactory getDaosFactory()

setDaosFactory

public void setDaosFactory(IDaosFactory daosFactory)

getDaoCommandExecutor

public IDaoCommandExecutor getDaoCommandExecutor()

setDaoCommandExecutor

public void setDaoCommandExecutor(IDaoCommandExecutor daoCommandExecutor)

getDaoCommandExecutorUnchecked

public IDaoCommandExecutorUnchecked getDaoCommandExecutorUnchecked()

setDaoCommandExecutorUnchecked

public void setDaoCommandExecutorUnchecked(IDaoCommandExecutorUnchecked daoCommandExecutorUnchecked)

setDataSource

public void setDataSource(javax.sql.DataSource dataSource)
Sets the default data source used by this PeristenceManager instance, and used by the default IPersistenceConfiguration instance too.

Parameters:
dataSource - The data source to set as default.

setDataSource

public void setDataSource(javax.sql.DataSource dataSource,
                          java.lang.Object configKey)
Sets the data source to be used by the IPersistenceConfiguration stored by configKey in the configuration factory of this PersistenceManager. This method call is equal to:

getConfigurationFactory().getOrCreateConfiguration(configKey).setDataSource(dataSource);

Parameters:
dataSource - The data source to set on the persistence configuration.
configKey - The key the persistence configuration is stored by in the configuration factory.

createDaos

public IDaos createDaos()
                 throws PersistenceException
Equivalent to calling getDaosFactory().createDao();

Returns:
An IDaos instance containing a connection obtained from the DataSource set on the default configuration of this PersistenceManager.
Throws:
PersistenceException - If opening the connection fails.

createDaos

public IDaos createDaos(java.sql.Connection connection)
Equivalent to calling getDaosFactory().createDao(connection);

Parameters:
connection - The Connection to use in the IDaos instance.
Returns:
An IDaos instance containing a connection obtained from the DataSource set on the default configuration of this PersistenceManager.

execute

public java.lang.Object execute(IDaoCommand daoCommand)
                         throws PersistenceException
Equivalent to calling getDaoCommandExecutor().execute(daoCommand);

Parameters:
daoCommand - The dao command to execute.
Returns:
The value returned from the dao command's execute method.
Throws:
PersistenceException - If anything goes wrong during the execution of the dao command.

executeTransaction

public java.lang.Object executeTransaction(IDaoCommand daoCommand)
                                    throws PersistenceException
Equivalent to calling getDaoCommandExecutor().executeTransaction(daoCommand);

Parameters:
daoCommand - The dao command to execute.
Returns:
The value returned from the dao command's execute method.
Throws:
PersistenceException - If anything goes wrong during the execution of the dao command.

executeUnchecked

public java.lang.Object executeUnchecked(IDaoCommand daoCommand)
Equivalent to calling getDaoCommandExecutorUnchecked().execute(daoCommand);

Parameters:
daoCommand - The dao command to execute.
Returns:
The value returned from the dao command's execute method.

executeTransactionUnchecked

public java.lang.Object executeTransactionUnchecked(IDaoCommand daoCommand)
Equivalent to calling getDaoCommandExecutorUnchecked().executeTransaction(daoCommand);

Parameters:
daoCommand - The dao command to execute.
Returns:
The value returned from the dao command's execute method.