org.springframework.orm.jdo
public class: LocalPersistenceManagerFactoryBean [javadoc |
source]
java.lang.Object
org.springframework.orm.jdo.LocalPersistenceManagerFactoryBean
All Implemented Interfaces:
PersistenceExceptionTranslator, BeanClassLoaderAware, DisposableBean, InitializingBean, FactoryBean
org.springframework.beans.factory.FactoryBean that creates a
JDO
javax.jdo.PersistenceManagerFactory . This is the usual way to
set up a shared JDO PersistenceManagerFactory in a Spring application context;
the PersistenceManagerFactory can then be passed to JDO-based DAOs via
dependency injection. Note that switching to a JNDI lookup or to a bean-style
PersistenceManagerFactory instance is just a matter of configuration!
Configuration settings can either be read from a properties file,
specified as "configLocation", or locally specified. Properties
specified as "jdoProperties" here will override any settings in a file.
On JDO 2.1, you may alternatively specify a "persistenceManagerFactoryName",
referring to a PMF definition in "META-INF/jdoconfig.xml"
(see #setPersistenceManagerFactoryName ).
NOTE: This class requires JDO 2.0 or higher, as of Spring 2.5.
This class also implements the
org.springframework.dao.support.PersistenceExceptionTranslator
interface, as autodetected by Spring's
org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor ,
for AOP-based translation of native exceptions to Spring DataAccessExceptions.
Hence, the presence of a LocalPersistenceManagerFactoryBean automatically enables
a PersistenceExceptionTranslationPostProcessor to translate JDO exceptions.
Alternative: Configuration of a PersistenceManagerFactory provider bean
As alternative to the properties-driven approach that this FactoryBean offers
(which is analogous to using the standard JDOHelper class with a Properties
object that is populated with standard JDO properties), you can set up an
instance of your PersistenceManagerFactory implementation class directly.
Like a DataSource, a PersistenceManagerFactory is encouraged to
support bean-style configuration, which makes it very easy to set up as
Spring-managed bean. The implementation class becomes the bean class;
the remaining properties are applied as bean properties (starting with
lower-case characters, in contrast to the corresponding JDO properties).
For example, in case of JPOX:
<bean id="persistenceManagerFactory" class="org.jpox.PersistenceManagerFactoryImpl" destroy-method="close">
<property name="connectionFactory" ref="dataSource"/>
<property name="nontransactionalRead" value="true"/>
</bean>
Note that such direct setup of a PersistenceManagerFactory implementation
is the only way to pass an external connection factory (i.e. a JDBC DataSource)
into a JDO PersistenceManagerFactory. With the standard properties-driven approach,
you can only use an internal connection pool or a JNDI DataSource.
The close() method is standardized in JDO; don't forget to
specify it as "destroy-method" for any PersistenceManagerFactory instance.
Note that this FactoryBean will automatically invoke close() for
the PersistenceManagerFactory that it creates, without any special configuration.
| Field Summary |
|---|
| protected final Log | logger | |
| Method from org.springframework.orm.jdo.LocalPersistenceManagerFactoryBean Summary: |
|---|
|
afterPropertiesSet, destroy, getJdoPropertyMap, getObject, getObjectType, isSingleton, newPersistenceManagerFactory, newPersistenceManagerFactory, setBeanClassLoader, setConfigLocation, setJdoDialect, setJdoProperties, setJdoPropertyMap, setPersistenceManagerFactoryName, translateExceptionIfPossible |
| Method from org.springframework.orm.jdo.LocalPersistenceManagerFactoryBean Detail: |
public void afterPropertiesSet() throws IllegalArgumentException, IOException, JDOException {
if (this.persistenceManagerFactoryName != null) {
if (this.configLocation != null || !this.jdoPropertyMap.isEmpty()) {
throw new IllegalStateException("'configLocation'/'jdoProperties' not supported in " +
"combination with 'persistenceManagerFactoryName' - specify one or the other, not both");
}
if (logger.isInfoEnabled()) {
logger.info("Building new JDO PersistenceManagerFactory for name '" +
this.persistenceManagerFactoryName + "'");
}
this.persistenceManagerFactory = newPersistenceManagerFactory(this.persistenceManagerFactoryName);
}
else {
Map mergedProps = new HashMap();
if (this.configLocation != null) {
if (logger.isInfoEnabled()) {
logger.info("Loading JDO config from [" + this.configLocation + "]");
}
mergedProps.putAll(PropertiesLoaderUtils.loadProperties(this.configLocation));
}
mergedProps.putAll(this.jdoPropertyMap);
// Build PersistenceManagerFactory instance.
logger.info("Building new JDO PersistenceManagerFactory");
this.persistenceManagerFactory = newPersistenceManagerFactory(mergedProps);
}
// Build default JdoDialect if none explicitly specified.
if (this.jdoDialect == null) {
this.jdoDialect = new DefaultJdoDialect(this.persistenceManagerFactory.getConnectionFactory());
}
}
Initialize the PersistenceManagerFactory for the given location. |
public void destroy() {
logger.info("Closing JDO PersistenceManagerFactory");
this.persistenceManagerFactory.close();
}
Close the PersistenceManagerFactory on bean factory shutdown. |
public Map getJdoPropertyMap() {
return this.jdoPropertyMap;
}
Allow Map access to the JDO properties to be passed to the JDOHelper,
with the option to add or override specific entries.
Useful for specifying entries directly, for example via
"jdoPropertyMap[myKey]". |
public Object getObject() {
return this.persistenceManagerFactory;
}
Return the singleton PersistenceManagerFactory. |
public Class getObjectType() {
return (this.persistenceManagerFactory != null ?
this.persistenceManagerFactory.getClass() : PersistenceManagerFactory.class);
}
|
public boolean isSingleton() {
return true;
}
|
protected PersistenceManagerFactory newPersistenceManagerFactory(String name) {
return JDOHelper.getPersistenceManagerFactory(name, this.beanClassLoader);
}
Subclasses can override this to perform custom initialization of the
PersistenceManagerFactory instance, creating it for the specified name.
The default implementation invokes JDOHelper's
getPersistenceManagerFactory(String) method.
A custom implementation could prepare the instance in a specific way,
or use a custom PersistenceManagerFactory implementation. |
protected PersistenceManagerFactory newPersistenceManagerFactory(Map props) {
return JDOHelper.getPersistenceManagerFactory(props, this.beanClassLoader);
}
Subclasses can override this to perform custom initialization of the
PersistenceManagerFactory instance, creating it via the given Properties
that got prepared by this LocalPersistenceManagerFactoryBean.
The default implementation invokes JDOHelper's
getPersistenceManagerFactory(Map) method.
A custom implementation could prepare the instance in a specific way,
or use a custom PersistenceManagerFactory implementation. |
public void setBeanClassLoader(ClassLoader beanClassLoader) {
this.beanClassLoader = beanClassLoader;
}
|
public void setConfigLocation(Resource configLocation) {
this.configLocation = configLocation;
}
|
public void setJdoDialect(JdoDialect jdoDialect) {
this.jdoDialect = jdoDialect;
}
Set the JDO dialect to use for the PersistenceExceptionTranslator
functionality of this factory.
Default is a DefaultJdoDialect based on the PersistenceManagerFactory's
underlying DataSource, if any. |
public void setJdoProperties(Properties jdoProperties) {
CollectionUtils.mergePropertiesIntoMap(jdoProperties, this.jdoPropertyMap);
}
Set JDO properties, such as"javax.jdo.PersistenceManagerFactoryClass".
Can be used to override values in a JDO properties config file,
or to specify all necessary properties locally.
Can be populated with a String "value" (parsed via PropertiesEditor)
or a "props" element in XML bean definitions. |
public void setJdoPropertyMap(Map jdoProperties) {
if (jdoProperties != null) {
this.jdoPropertyMap.putAll(jdoProperties);
}
}
|
public void setPersistenceManagerFactoryName(String persistenceManagerFactoryName) {
this.persistenceManagerFactoryName = persistenceManagerFactoryName;
}
Specify the name of the desired PersistenceManagerFactory.
This may either be a properties resource in the classpath if such a resource exists
(JDO 2.0), or a PMF definition with that name from "META-INF/jdoconfig.xml" (JDO 2.1),
or a JPA EntityManagerFactory cast to a PersistenceManagerFactory based on the
persistence-unit name from "META-INF/persistence.xml" (JDO 2.1 / JPA 1.0).
Default is none: Either 'persistenceManagerFactoryName' or 'configLocation'
or 'jdoProperties' needs to be specified. |
public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
if (ex instanceof JDOException) {
if (this.jdoDialect != null) {
return this.jdoDialect.translateException((JDOException) ex);
}
else {
return PersistenceManagerFactoryUtils.convertJdoAccessException((JDOException) ex);
}
}
return null;
}
Implementation of the PersistenceExceptionTranslator interface,
as autodetected by Spring's PersistenceExceptionTranslationPostProcessor.
Converts the exception if it is a JDOException, preferably using a specified
JdoDialect. Else returns null to indicate an unknown exception. |