Removed deprecated JdoTemplate and JdoInterceptor classes; formally requiring JDO 3.0+ now

This commit is contained in:
Juergen Hoeller 2013-03-29 13:17:13 +01:00
parent cc0ea4a824
commit 7ceb02257e
21 changed files with 94 additions and 2695 deletions

View File

@ -16,18 +16,13 @@
package org.springframework.orm.jdo;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.SQLException;
import javax.jdo.Constants;
import javax.jdo.JDOException;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.Transaction;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.support.PersistenceExceptionTranslator;
import org.springframework.jdbc.datasource.ConnectionHandle;
@ -35,24 +30,21 @@ import org.springframework.jdbc.support.JdbcUtils;
import org.springframework.jdbc.support.SQLExceptionTranslator;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionException;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
/**
* Default implementation of the {@link JdoDialect} interface.
* Requires JDO 2.0; explicitly supports JDO API features up until 3.0.
* Used as default dialect by {@link JdoAccessor} and {@link JdoTransactionManager}.
* As of Spring 4.0, designed for JDO 3.0 (or rather, semantics beyond JDO 3.0).
* Used as default dialect by {@link JdoTransactionManager}.
*
* <p>Simply begins a standard JDO transaction in {@code beginTransaction}.
* Returns a handle for a JDO2 DataStoreConnection on {@code getJdbcConnection}.
* Calls the corresponding JDO2 PersistenceManager operation on {@code flush}
* Translates {@code applyQueryTimeout} to JDO 3.0's {@code setTimeoutMillis}.
* Returns a handle for a JDO DataStoreConnection on {@code getJdbcConnection}.
* Calls the corresponding JDO PersistenceManager operation on {@code flush}
* Uses a Spring SQLExceptionTranslator for exception translation, if applicable.
*
* <p>Note that, even with JDO2, vendor-specific subclasses are still necessary
* <p>Note that, even with JDO 3.0, vendor-specific subclasses are still necessary
* for special transaction semantics and more sophisticated exception translation.
* Furthermore, vendor-specific subclasses are encouraged to expose the native JDBC
* Connection on {@code getJdbcConnection}, rather than JDO2's wrapper handle.
* Connection on {@code getJdbcConnection}, rather than JDO 3.0's wrapper handle.
*
* <p>This class also implements the PersistenceExceptionTranslator interface,
* as autodetected by Spring's PersistenceExceptionTranslationPostProcessor,
@ -63,18 +55,11 @@ import org.springframework.util.ReflectionUtils;
* @author Juergen Hoeller
* @since 1.1
* @see #setJdbcExceptionTranslator
* @see JdoAccessor#setJdoDialect
* @see JdoTransactionManager#setJdoDialect
* @see org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor
*/
public class DefaultJdoDialect implements JdoDialect, PersistenceExceptionTranslator {
// JDO 3.0 setTimeoutMillis method available?
private static final Method setTimeoutMillisMethod =
ClassUtils.getMethodIfAvailable(Query.class, "setTimeoutMillis", Integer.class);
protected final Log logger = LogFactory.getLog(getClass());
private SQLExceptionTranslator jdbcExceptionTranslator;
@ -174,18 +159,14 @@ public class DefaultJdoDialect implements JdoDialect, PersistenceExceptionTransl
}
/**
* This implementation returns a DataStoreConnectionHandle for JDO2,
* which will also work on JDO1 until actually accessing the JDBC Connection.
* <p>For pre-JDO2 implementations, override this method to return the
* Connection through the corresponding vendor-specific mechanism, or {@code null}
* if the Connection is not retrievable.
* <p><b>NOTE:</b> A JDO2 DataStoreConnection is always a wrapper,
* This implementation returns a DataStoreConnectionHandle for JDO.
* <p><b>NOTE:</b> A JDO DataStoreConnection is always a wrapper,
* never the native JDBC Connection. If you need access to the native JDBC
* Connection (or the connection pool handle, to be unwrapped via a Spring
* NativeJdbcExtractor), override this method to return the native
* Connection through the corresponding vendor-specific mechanism.
* <p>A JDO2 DataStoreConnection is only "borrowed" from the PersistenceManager:
* it needs to be returned as early as possible. Effectively, JDO2 requires the
* <p>A JDO DataStoreConnection is only "borrowed" from the PersistenceManager:
* it needs to be returned as early as possible. Effectively, JDO requires the
* fetched Connection to be closed before continuing PersistenceManager work.
* For this reason, the exposed ConnectionHandle eagerly releases its JDBC
* Connection at the end of each JDBC data access operation (that is, on
@ -212,25 +193,9 @@ public class DefaultJdoDialect implements JdoDialect, PersistenceExceptionTransl
throws JDOException, SQLException {
}
/**
* This implementation applies a JDO 3.0 query timeout, if available. Otherwise,
* it sets the JPA 2.0 query hints "javax.persistence.lock.timeout" and
* "javax.persistence.query.timeout", assuming that JDO providers are often
* JPA providers as well.
*/
public void applyQueryTimeout(Query query, int remainingTimeInSeconds) throws JDOException {
if (setTimeoutMillisMethod != null) {
ReflectionUtils.invokeMethod(setTimeoutMillisMethod, query, remainingTimeInSeconds);
}
else {
query.addExtension("javax.persistence.lock.timeout", remainingTimeInSeconds);
query.addExtension("javax.persistence.query.timeout", remainingTimeInSeconds);
}
}
//-----------------------------------------------------------------------------------
// Hook for exception translation (used by JdoTransactionManager and JdoTemplate)
// Hook for exception translation (used by JdoTransactionManager)
//-----------------------------------------------------------------------------------
/**
@ -273,9 +238,9 @@ public class DefaultJdoDialect implements JdoDialect, PersistenceExceptionTransl
/**
* ConnectionHandle implementation that fetches a new JDO2 DataStoreConnection
* ConnectionHandle implementation that fetches a new JDO DataStoreConnection
* for every {@code getConnection} call and closes the Connection on
* {@code releaseConnection}. This is necessary because JDO2 requires the
* {@code releaseConnection}. This is necessary because JDO requires the
* fetched Connection to be closed before continuing PersistenceManager work.
* @see javax.jdo.PersistenceManager#getDataStoreConnection()
*/

View File

@ -1,172 +0,0 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.orm.jdo;
import javax.jdo.JDOException;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.dao.DataAccessException;
/**
* Base class for JdoTemplate and JdoInterceptor, defining common
* properties such as PersistenceManagerFactory and flushing behavior.
*
* <p>Note: With JDO, modifications to persistent objects are just possible
* within a transaction (in contrast to Hibernate). Therefore, eager flushing
* will just get applied when in a transaction. Furthermore, there is no
* explicit notion of flushing never, as this would not imply a performance
* gain due to JDO's field interception mechanism (which doesn't involve
* the overhead of snapshot comparisons).
*
* <p>Eager flushing is just available for specific JDO providers.
* You need to a corresponding JdoDialect to make eager flushing work.
*
* <p>Not intended to be used directly. See JdoTemplate and JdoInterceptor.
*
* @author Juergen Hoeller
* @since 02.11.2003
* @see JdoTemplate
* @see JdoInterceptor
* @see #setFlushEager
* @deprecated as of Spring 3.1, in favor of native PersistenceManager usage
* (see {@link TransactionAwarePersistenceManagerFactoryProxy} and
* {@link org.springframework.orm.jdo.support.SpringPersistenceManagerProxyBean})
*/
@Deprecated
public abstract class JdoAccessor implements InitializingBean {
/** Logger available to subclasses */
protected final Log logger = LogFactory.getLog(getClass());
private PersistenceManagerFactory persistenceManagerFactory;
private JdoDialect jdoDialect;
private boolean flushEager = false;
/**
* Set the JDO PersistenceManagerFactory that should be used to create
* PersistenceManagers.
*/
public void setPersistenceManagerFactory(PersistenceManagerFactory pmf) {
this.persistenceManagerFactory = pmf;
}
/**
* Return the JDO PersistenceManagerFactory that should be used to create
* PersistenceManagers.
*/
public PersistenceManagerFactory getPersistenceManagerFactory() {
return this.persistenceManagerFactory;
}
/**
* Set the JDO dialect to use for this accessor.
* <p>The dialect object can be used to retrieve the underlying JDBC
* connection and to eagerly flush changes to the database.
* <p>Default is a DefaultJdoDialect based on the PersistenceManagerFactory's
* underlying DataSource, if any.
*/
public void setJdoDialect(JdoDialect jdoDialect) {
this.jdoDialect = jdoDialect;
}
/**
* Return the JDO dialect to use for this accessor.
* <p>Creates a default one for the specified PersistenceManagerFactory if none set.
*/
public JdoDialect getJdoDialect() {
if (this.jdoDialect == null) {
this.jdoDialect = new DefaultJdoDialect();
}
return this.jdoDialect;
}
/**
* Set if this accessor should flush changes to the database eagerly.
* <p>Eager flushing leads to immediate synchronization with the database,
* even if in a transaction. This causes inconsistencies to show up and throw
* a respective exception immediately, and JDBC access code that participates
* in the same transaction will see the changes as the database is already
* aware of them then. But the drawbacks are:
* <ul>
* <li>additional communication roundtrips with the database, instead of a
* single batch at transaction commit;
* <li>the fact that an actual database rollback is needed if the JDO
* transaction rolls back (due to already submitted SQL statements).
* </ul>
*/
public void setFlushEager(boolean flushEager) {
this.flushEager = flushEager;
}
/**
* Return if this accessor should flush changes to the database eagerly.
*/
public boolean isFlushEager() {
return this.flushEager;
}
/**
* Eagerly initialize the JDO dialect, creating a default one
* for the specified PersistenceManagerFactory if none set.
*/
public void afterPropertiesSet() {
if (getPersistenceManagerFactory() == null) {
throw new IllegalArgumentException("Property 'persistenceManagerFactory' is required");
}
// Build default JdoDialect if none explicitly specified.
if (this.jdoDialect == null) {
this.jdoDialect = new DefaultJdoDialect(getPersistenceManagerFactory().getConnectionFactory());
}
}
/**
* Flush the given JDO persistence manager if necessary.
* @param pm the current JDO PersistenceManager
* @param existingTransaction if executing within an existing transaction
* (within an existing JDO PersistenceManager that won't be closed immediately)
* @throws JDOException in case of JDO flushing errors
*/
protected void flushIfNecessary(PersistenceManager pm, boolean existingTransaction) throws JDOException {
if (isFlushEager()) {
logger.debug("Eagerly flushing JDO persistence manager");
pm.flush();
}
}
/**
* Convert the given JDOException to an appropriate exception from the
* {@code org.springframework.dao} hierarchy.
* <p>Default implementation delegates to the JdoDialect.
* May be overridden in subclasses.
* @param ex JDOException that occured
* @return the corresponding DataAccessException instance
* @see JdoDialect#translateException
*/
public DataAccessException convertJdoAccessException(JDOException ex) {
return getJdoDialect().translateException(ex);
}
}

View File

@ -1,73 +0,0 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.orm.jdo;
import javax.jdo.JDOException;
import javax.jdo.PersistenceManager;
/**
* Callback interface for JDO code. To be used with {@link JdoTemplate}'s
* execution methods, often as anonymous classes within a method implementation.
* A typical implementation will call PersistenceManager CRUD to perform
* some operations on persistent objects.
*
* <p>Note that JDO works on bytecode-modified Java objects, to be able to
* perform dirty detection on each modification of a persistent instance field.
* In contrast to Hibernate, using returned objects outside of an active
* PersistenceManager poses a problem: To be able to read and modify fields
* e.g. in a web GUI, one has to explicitly make the instances "transient".
* Reassociation with a new PersistenceManager, e.g. for updates when coming
* back from the GUI, isn't possible, as the JDO instances have lost their
* identity when turned transient. This means that either value objects have
* to be used as parameters, or the contents of the outside-modified instance
* have to be copied to a freshly loaded active instance on reassociation.
*
* @author Juergen Hoeller
* @since 03.06.2003
* @see JdoTemplate
* @see JdoTransactionManager
* @deprecated as of Spring 3.1, in favor of native PersistenceManager usage
* (see {@link TransactionAwarePersistenceManagerFactoryProxy} and
* {@link org.springframework.orm.jdo.support.SpringPersistenceManagerProxyBean})
*/
@Deprecated
public interface JdoCallback<T> {
/**
* Gets called by {@code JdoTemplate.execute} with an active JDO
* {@code PersistenceManager}. Does not need to care about activating
* or closing the {@code PersistenceManager}, or handling transactions.
*
* <p>Note that JDO callback code will not flush any modifications to the
* database if not executed within a transaction. Thus, you need to make
* sure that JdoTransactionManager has initiated a JDO transaction when
* the callback gets called, at least if you want to write to the database.
*
* <p>Allows for returning a result object created within the callback,
* i.e. a domain object or a collection of domain objects.
* A thrown custom RuntimeException is treated as an application exception:
* It gets propagated to the caller of the template.
*
* @param pm active PersistenceManager
* @return a result object, or {@code null} if none
* @throws JDOException if thrown by the JDO API
* @see JdoTemplate#execute
* @see JdoTemplate#executeFind
*/
T doInJdo(PersistenceManager pm) throws JDOException;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -19,7 +19,6 @@ package org.springframework.orm.jdo;
import java.sql.SQLException;
import javax.jdo.JDOException;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.Transaction;
import org.springframework.dao.DataAccessException;
@ -32,8 +31,8 @@ import org.springframework.transaction.TransactionException;
* in particular regarding transaction management and exception translation. To be
* implemented for specific JDO providers such as JPOX, Kodo, Lido, Versant Open Access.
*
* <p>JDO 2.0 defines standard ways for most of the functionality covered here.
* Hence, Spring's {@link DefaultJdoDialect} uses the corresponding JDO 2.0 methods
* <p>JDO 3.0 defines standard ways for most of the functionality covered here.
* Hence, Spring's {@link DefaultJdoDialect} uses the corresponding JDO 3.0 methods
* by default, to be overridden in a vendor-specific fashion if necessary.
* Vendor-specific subclasses of {@link DefaultJdoDialect} are still required for special
* transaction semantics and more sophisticated exception translation (if needed).
@ -46,7 +45,6 @@ import org.springframework.transaction.TransactionException;
* @author Juergen Hoeller
* @since 02.11.2003
* @see JdoTransactionManager#setJdoDialect
* @see JdoAccessor#setJdoDialect
* @see DefaultJdoDialect
*/
public interface JdoDialect {
@ -144,19 +142,9 @@ public interface JdoDialect {
void releaseJdbcConnection(ConnectionHandle conHandle, PersistenceManager pm)
throws JDOException, SQLException;
/**
* Apply the given timeout to the given JDO query object.
* <p>Invoked with the remaining time of a specified transaction timeout, if any.
* @param query the JDO query object to apply the timeout to
* @param timeout the timeout value (seconds) to apply
* @throws JDOException if thrown by JDO methods
* @see JdoTemplate#prepareQuery
*/
void applyQueryTimeout(Query query, int timeout) throws JDOException;
//-----------------------------------------------------------------------------------
// Hook for exception translation (used by JdoTransactionManager and JdoTemplate)
// Hook for exception translation (used by JdoTransactionManager)
//-----------------------------------------------------------------------------------
/**
@ -171,7 +159,6 @@ public interface JdoDialect {
* in a database-specific fashion.
* @param ex the JDOException thrown
* @return the corresponding DataAccessException (must not be {@code null})
* @see JdoAccessor#convertJdoAccessException
* @see JdoTransactionManager#convertJdoAccessException
* @see PersistenceManagerFactoryUtils#convertJdoAccessException
* @see org.springframework.dao.DataIntegrityViolationException

View File

@ -1,128 +0,0 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.orm.jdo;
import javax.jdo.JDOException;
import javax.jdo.PersistenceManager;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.transaction.support.TransactionSynchronizationManager;
/**
* This interceptor binds a new JDO PersistenceManager to the thread before a method
* call, closing and removing it afterwards in case of any method outcome.
* If there already is a pre-bound PersistenceManager (e.g. from JdoTransactionManager,
* or from a surrounding JDO-intercepted method), the interceptor simply participates in it.
*
* <p>Application code must retrieve a JDO PersistenceManager via the
* {@code PersistenceManagerFactoryUtils.getPersistenceManager} method,
* to be able to detect a thread-bound PersistenceManager. It is preferable to use
* {@code getPersistenceManager} with allowCreate=false, if the code relies on
* the interceptor to provide proper PersistenceManager handling. Typically, the code
* will look like as follows:
*
* <pre>
* public void doSomeDataAccessAction() {
* PersistenceManager pm = PersistenceManagerFactoryUtils.getPersistenceManager(this.pmf, false);
* ...
* }</pre>
*
* <p>Note that this interceptor automatically translates JDOExceptions, via
* delegating to the {@code PersistenceManagerFactoryUtils.convertJdoAccessException}
* method that converts them to exceptions that are compatible with the
* {@code org.springframework.dao} exception hierarchy (like JdoTemplate does).
* This can be turned off if the raw exceptions are preferred.
*
* <p>This class can be considered a declarative alternative to JdoTemplate's
* callback approach. The advantages are:
* <ul>
* <li>no anonymous classes necessary for callback implementations;
* <li>the possibility to throw any application exceptions from within data access code.
* </ul>
*
* <p>The drawback is the dependency on interceptor configuration. However, note
* that this interceptor is usually <i>not</i> necessary in scenarios where the
* data access code always executes within transactions. A transaction will always
* have a thread-bound PersistenceManager in the first place, so adding this interceptor
* to the configuration just adds value when fine-tuning PersistenceManager settings
* like the flush mode - or when relying on exception translation.
*
* @author Juergen Hoeller
* @since 13.06.2003
* @see PersistenceManagerFactoryUtils#getPersistenceManager
* @see JdoTransactionManager
* @see JdoTemplate
* @deprecated as of Spring 3.1, in favor of native PersistenceManager usage
* and AOP-driven exception translation through
* {@link org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor}
*/
@Deprecated
public class JdoInterceptor extends JdoAccessor implements MethodInterceptor {
private boolean exceptionConversionEnabled = true;
/**
* Set whether to convert any JDOException raised to a Spring DataAccessException,
* compatible with the {@code org.springframework.dao} exception hierarchy.
* <p>Default is "true". Turn this flag off to let the caller receive raw exceptions
* as-is, without any wrapping.
* @see org.springframework.dao.DataAccessException
*/
public void setExceptionConversionEnabled(boolean exceptionConversionEnabled) {
this.exceptionConversionEnabled = exceptionConversionEnabled;
}
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
boolean existingTransaction = false;
PersistenceManager pm = PersistenceManagerFactoryUtils.getPersistenceManager(getPersistenceManagerFactory(), true);
if (TransactionSynchronizationManager.hasResource(getPersistenceManagerFactory())) {
logger.debug("Found thread-bound PersistenceManager for JDO interceptor");
existingTransaction = true;
}
else {
logger.debug("Using new PersistenceManager for JDO interceptor");
TransactionSynchronizationManager.bindResource(getPersistenceManagerFactory(), new PersistenceManagerHolder(pm));
}
try {
Object retVal = methodInvocation.proceed();
flushIfNecessary(pm, existingTransaction);
return retVal;
}
catch (JDOException ex) {
if (this.exceptionConversionEnabled) {
throw convertJdoAccessException(ex);
}
else {
throw ex;
}
}
finally {
if (existingTransaction) {
logger.debug("Not closing pre-bound JDO PersistenceManager after interceptor");
}
else {
TransactionSynchronizationManager.unbindResource(getPersistenceManagerFactory());
PersistenceManagerFactoryUtils.releasePersistenceManager(pm, getPersistenceManagerFactory());
}
}
}
}

View File

@ -1,420 +0,0 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.orm.jdo;
import java.util.Collection;
import java.util.Map;
import org.springframework.dao.DataAccessException;
/**
* Interface that specifies a basic set of JDO operations,
* implemented by {@link JdoTemplate}. Not often used, but a useful
* option to enhance testability, as it can easily be mocked or stubbed.
*
* <p>Defines {@code JdoTemplate}'s data access methods that mirror
* various JDO {@link javax.jdo.PersistenceManager} methods. Users are
* strongly encouraged to read the JDO {@code PersistenceManager}
* javadocs for details on the semantics of those methods.
*
* <p>Note that lazy loading will just work with an open JDO
* {@code PersistenceManager}, either within a managed transaction or within
* {@link org.springframework.orm.jdo.support.OpenPersistenceManagerInViewFilter}/
* {@link org.springframework.orm.jdo.support.OpenPersistenceManagerInViewInterceptor}.
* Furthermore, some operations just make sense within transactions,
* for example: {@code evict}, {@code evictAll}, {@code flush}.
*
* <p>Updated to build on JDO 2.0 or higher, as of Spring 2.5.
*
* @author Juergen Hoeller
* @since 1.1
* @see JdoTemplate
* @see javax.jdo.PersistenceManager
* @see JdoTransactionManager
* @see JdoDialect
* @see org.springframework.orm.jdo.support.OpenPersistenceManagerInViewFilter
* @see org.springframework.orm.jdo.support.OpenPersistenceManagerInViewInterceptor
* @deprecated as of Spring 3.1, in favor of native PersistenceManager usage
* (see {@link TransactionAwarePersistenceManagerFactoryProxy} and
* {@link org.springframework.orm.jdo.support.SpringPersistenceManagerProxyBean})
*/
@Deprecated
public interface JdoOperations {
/**
* Execute the action specified by the given action object within a
* PersistenceManager. Application exceptions thrown by the action object
* get propagated to the caller (can only be unchecked). JDO exceptions
* are transformed into appropriate DAO ones. Allows for returning a
* result object, i.e. a domain object or a collection of domain objects.
* <p>Note: Callback code is not supposed to handle transactions itself!
* Use an appropriate transaction manager like JdoTransactionManager.
* @param action callback object that specifies the JDO action
* @return a result object returned by the action, or {@code null}
* @throws org.springframework.dao.DataAccessException in case of JDO errors
* @see JdoTransactionManager
* @see javax.jdo.PersistenceManager
*/
<T> T execute(JdoCallback<T> action) throws DataAccessException;
/**
* Execute the specified action assuming that the result object is a
* Collection. This is a convenience method for executing JDO queries
* within an action.
* @param action callback object that specifies the JDO action
* @return a Collection result returned by the action, or {@code null}
* @throws org.springframework.dao.DataAccessException in case of JDO errors
*/
Collection executeFind(JdoCallback<?> action) throws DataAccessException;
//-------------------------------------------------------------------------
// Convenience methods for load, save, delete
//-------------------------------------------------------------------------
/**
* Return the persistent instance with the given JDO object id,
* throwing an exception if not found.
* <p>A JDO object id identifies both the persistent class and the id
* within the namespace of that class.
* @param objectId a JDO object id of the persistent instance
* @return the persistent instance
* @throws org.springframework.orm.ObjectRetrievalFailureException if not found
* @throws org.springframework.dao.DataAccessException in case of JDO errors
* @see javax.jdo.PersistenceManager#getObjectById(Object, boolean)
*/
Object getObjectById(Object objectId) throws DataAccessException;
/**
* Return the persistent instance of the given entity class
* with the given id value, throwing an exception if not found.
* <p>The given id value is typically just unique within the namespace
* of the persistent class. Its toString value must correspond to the
* toString value of the corresponding JDO object id.
* <p>Usually, the passed-in value will have originated from the primary
* key field of a persistent object that uses JDO's application identity.
* @param entityClass a persistent class
* @param idValue an id value of the persistent instance
* @return the persistent instance
* @throws org.springframework.orm.ObjectRetrievalFailureException if not found
* @throws org.springframework.dao.DataAccessException in case of JDO errors
* @see javax.jdo.PersistenceManager#getObjectById(Object, boolean)
* @see javax.jdo.PersistenceManager#getObjectById(Class, Object)
*/
<T> T getObjectById(Class<T> entityClass, Object idValue) throws DataAccessException;
/**
* Remove the given object from the PersistenceManager cache.
* @param entity the persistent instance to evict
* @throws org.springframework.dao.DataAccessException in case of JDO errors
* @see javax.jdo.PersistenceManager#evict(Object)
*/
void evict(Object entity) throws DataAccessException;
/**
* Remove all given objects from the PersistenceManager cache.
* @param entities the persistent instances to evict
* @throws org.springframework.dao.DataAccessException in case of JDO errors
* @see javax.jdo.PersistenceManager#evictAll(java.util.Collection)
*/
void evictAll(Collection entities) throws DataAccessException;
/**
* Remove all objects from the PersistenceManager cache.
* @throws org.springframework.dao.DataAccessException in case of JDO errors
* @see javax.jdo.PersistenceManager#evictAll()
*/
void evictAll() throws DataAccessException;
/**
* Re-read the state of the given persistent instance.
* @param entity the persistent instance to re-read
* @throws org.springframework.dao.DataAccessException in case of JDO errors
* @see javax.jdo.PersistenceManager#refresh(Object)
*/
void refresh(Object entity) throws DataAccessException;
/**
* Re-read the state of all given persistent instances.
* @param entities the persistent instances to re-read
* @throws org.springframework.dao.DataAccessException in case of JDO errors
* @see javax.jdo.PersistenceManager#refreshAll(java.util.Collection)
*/
void refreshAll(Collection entities) throws DataAccessException;
/**
* Re-read the state of all persistent instances.
* @throws org.springframework.dao.DataAccessException in case of JDO errors
* @see javax.jdo.PersistenceManager#refreshAll()
*/
void refreshAll() throws DataAccessException;
/**
* Make the given transient instance persistent.
* Attach the given entity if the instance is detached.
* @param entity the transient instance to make persistent
* @return the persistent instance
* @throws org.springframework.dao.DataAccessException in case of JDO errors
* @see javax.jdo.PersistenceManager#makePersistent(Object)
*/
<T> T makePersistent(T entity) throws DataAccessException;
/**
* Make the given transient instances persistent.
* Attach the given entities if the instances are detached.
* @param entities the transient instances to make persistent
* @return the persistent instances
* @throws org.springframework.dao.DataAccessException in case of JDO errors
* @see javax.jdo.PersistenceManager#makePersistentAll(java.util.Collection)
*/
<T> Collection<T> makePersistentAll(Collection<T> entities) throws DataAccessException;
/**
* Delete the given persistent instance.
* @param entity the persistent instance to delete
* @throws org.springframework.dao.DataAccessException in case of JDO errors
* @see javax.jdo.PersistenceManager#deletePersistent(Object)
*/
void deletePersistent(Object entity) throws DataAccessException;
/**
* Delete all given persistent instances.
* <p>This can be combined with any of the find methods to delete by query
* in two lines of code.
* @param entities the persistent instances to delete
* @throws org.springframework.dao.DataAccessException in case of JDO errors
* @see javax.jdo.PersistenceManager#deletePersistentAll(java.util.Collection)
*/
void deletePersistentAll(Collection entities) throws DataAccessException;
/**
* Detach a copy of the given persistent instance from the current JDO transaction,
* for use outside a JDO transaction (for example, as web form object).
* @param entity the persistent instance to detach
* @return the corresponding detached instance
* @see javax.jdo.PersistenceManager#detachCopy(Object)
*/
<T> T detachCopy(T entity);
/**
* Detach copies of the given persistent instances from the current JDO transaction,
* for use outside a JDO transaction (for example, as web form objects).
* @param entities the persistent instances to detach
* @return the corresponding detached instances
* @see javax.jdo.PersistenceManager#detachCopyAll(Collection)
*/
<T> Collection<T> detachCopyAll(Collection<T> entities);
/**
* Flush all transactional modifications to the database.
* <p>Only invoke this for selective eager flushing, for example when JDBC code
* needs to see certain changes within the same transaction. Else, it's preferable
* to rely on auto-flushing at transaction completion.
* @throws org.springframework.dao.DataAccessException in case of JDO errors
* @see javax.jdo.PersistenceManager#flush()
*/
void flush() throws DataAccessException;
//-------------------------------------------------------------------------
// Convenience finder methods
//-------------------------------------------------------------------------
/**
* Find all persistent instances of the given class.
* @param entityClass a persistent class
* @return the persistent instances
* @throws org.springframework.dao.DataAccessException in case of JDO errors
* @see javax.jdo.PersistenceManager#newQuery(Class)
*/
<T> Collection<T> find(Class<T> entityClass) throws DataAccessException;
/**
* Find all persistent instances of the given class that match the given
* JDOQL filter.
* @param entityClass a persistent class
* @param filter the JDOQL filter to match (or {@code null} if none)
* @return the persistent instances
* @throws org.springframework.dao.DataAccessException in case of JDO errors
* @see javax.jdo.PersistenceManager#newQuery(Class, String)
*/
<T> Collection<T> find(Class<T> entityClass, String filter) throws DataAccessException;
/**
* Find all persistent instances of the given class that match the given
* JDOQL filter, with the given result ordering.
* @param entityClass a persistent class
* @param filter the JDOQL filter to match (or {@code null} if none)
* @param ordering the ordering of the result (or {@code null} if none)
* @return the persistent instances
* @throws org.springframework.dao.DataAccessException in case of JDO errors
* @see javax.jdo.PersistenceManager#newQuery(Class, String)
* @see javax.jdo.Query#setOrdering
*/
<T> Collection<T> find(Class<T> entityClass, String filter, String ordering) throws DataAccessException;
/**
* Find all persistent instances of the given class that match the given
* JDOQL filter, using the given parameter declarations and parameter values.
* @param entityClass a persistent class
* @param filter the JDOQL filter to match
* @param parameters the JDOQL parameter declarations
* @param values the corresponding parameter values
* @return the persistent instances
* @throws org.springframework.dao.DataAccessException in case of JDO errors
* @see javax.jdo.PersistenceManager#newQuery(Class, String)
* @see javax.jdo.Query#declareParameters
* @see javax.jdo.Query#executeWithArray
*/
<T> Collection<T> find(Class<T> entityClass, String filter, String parameters, Object... values)
throws DataAccessException;
/**
* Find all persistent instances of the given class that match the given
* JDOQL filter, using the given parameter declarations and parameter values,
* with the given result ordering.
* @param entityClass a persistent class
* @param filter the JDOQL filter to match
* @param parameters the JDOQL parameter declarations
* @param values the corresponding parameter values
* @param ordering the ordering of the result (or {@code null} if none)
* @return the persistent instances
* @throws org.springframework.dao.DataAccessException in case of JDO errors
* @see javax.jdo.PersistenceManager#newQuery(Class, String)
* @see javax.jdo.Query#declareParameters
* @see javax.jdo.Query#executeWithArray
* @see javax.jdo.Query#setOrdering
*/
<T> Collection<T> find(
Class<T> entityClass, String filter, String parameters, Object[] values, String ordering)
throws DataAccessException;
/**
* Find all persistent instances of the given class that match the given
* JDOQL filter, using the given parameter declarations and parameter values.
* @param entityClass a persistent class
* @param filter the JDOQL filter to match
* @param parameters the JDOQL parameter declarations
* @param values a Map with parameter names as keys and parameter values
* @return the persistent instances
* @throws org.springframework.dao.DataAccessException in case of JDO errors
* @see javax.jdo.PersistenceManager#newQuery(Class, String)
* @see javax.jdo.Query#declareParameters
* @see javax.jdo.Query#executeWithMap
*/
<T> Collection<T> find(Class<T> entityClass, String filter, String parameters, Map<String, ?> values)
throws DataAccessException;
/**
* Find all persistent instances of the given class that match the given
* JDOQL filter, using the given parameter declarations and parameter values,
* with the given result ordering.
* @param entityClass a persistent class
* @param filter the JDOQL filter to match
* @param parameters the JDOQL parameter declarations
* @param values a Map with parameter names as keys and parameter values
* @param ordering the ordering of the result (or {@code null} if none)
* @return the persistent instances
* @throws org.springframework.dao.DataAccessException in case of JDO errors
* @see javax.jdo.PersistenceManager#newQuery(Class, String)
* @see javax.jdo.Query#declareParameters
* @see javax.jdo.Query#executeWithMap
* @see javax.jdo.Query#setOrdering
*/
<T> Collection<T> find(
Class<T> entityClass, String filter, String parameters, Map<String, ?> values, String ordering)
throws DataAccessException;
/**
* Find persistent instances through the given query object
* in the specified query language.
* @param language the query language ({@code javax.jdo.Query#JDOQL}
* or {@code javax.jdo.Query#SQL}, for example)
* @param queryObject the query object for the specified language
* @return the persistent instances
* @throws org.springframework.dao.DataAccessException in case of JDO errors
* @see javax.jdo.PersistenceManager#newQuery(String, Object)
* @see javax.jdo.Query#JDOQL
* @see javax.jdo.Query#SQL
*/
Collection find(String language, Object queryObject) throws DataAccessException;
/**
* Find persistent instances through the given single-string JDOQL query.
* @param queryString the single-string JDOQL query
* @return the persistent instances
* @throws org.springframework.dao.DataAccessException in case of JDO errors
* @see javax.jdo.PersistenceManager#newQuery(String)
*/
Collection find(String queryString) throws DataAccessException;
/**
* Find persistent instances through the given single-string JDOQL query.
* @param queryString the single-string JDOQL query
* @param values the corresponding parameter values
* @return the persistent instances
* @throws org.springframework.dao.DataAccessException in case of JDO errors
* @see javax.jdo.PersistenceManager#newQuery(String)
*/
Collection find(String queryString, Object... values) throws DataAccessException;
/**
* Find persistent instances through the given single-string JDOQL query.
* @param queryString the single-string JDOQL query
* @param values a Map with parameter names as keys and parameter values
* @return the persistent instances
* @throws org.springframework.dao.DataAccessException in case of JDO errors
* @see javax.jdo.PersistenceManager#newQuery(String)
*/
Collection find(String queryString, Map<String, ?> values) throws DataAccessException;
/**
* Find persistent instances through the given named query.
* @param entityClass a persistent class
* @param queryName the name of the query
* @return the persistent instances
* @throws org.springframework.dao.DataAccessException in case of JDO errors
* @see javax.jdo.PersistenceManager#newNamedQuery(Class, String)
*/
<T> Collection<T> findByNamedQuery(Class<T> entityClass, String queryName)
throws DataAccessException;
/**
* Find persistent instances through the given named query.
* @param entityClass a persistent class
* @param queryName the name of the query
* @param values the corresponding parameter values
* @return the persistent instances
* @throws org.springframework.dao.DataAccessException in case of JDO errors
* @see javax.jdo.PersistenceManager#newNamedQuery(Class, String)
*/
<T> Collection<T> findByNamedQuery(Class<T> entityClass, String queryName, Object... values)
throws DataAccessException;
/**
* Find persistent instances through the given named query.
* @param entityClass a persistent class
* @param queryName the name of the query
* @param values a Map with parameter names as keys and parameter values
* @return the persistent instances
* @throws org.springframework.dao.DataAccessException in case of JDO errors
* @see javax.jdo.PersistenceManager#newNamedQuery(Class, String)
*/
<T> Collection<T> findByNamedQuery(Class<T> entityClass, String queryName, Map<String, ?> values)
throws DataAccessException;
}

View File

@ -1,618 +0,0 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.orm.jdo;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Collection;
import java.util.Map;
import javax.jdo.JDOException;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
/**
* Helper class that simplifies JDO data access code, and converts
* JDOExceptions into Spring DataAccessExceptions, following the
* {@code org.springframework.dao} exception hierarchy.
*
* <p>The central method is {@code execute}, supporting JDO access code
* implementing the {@link JdoCallback} interface. It provides JDO PersistenceManager
* handling such that neither the JdoCallback implementation nor the calling
* code needs to explicitly care about retrieving/closing PersistenceManagers,
* or handling JDO lifecycle exceptions.
*
* <p>Typically used to implement data access or business logic services that
* use JDO within their implementation but are JDO-agnostic in their interface.
* The latter or code calling the latter only have to deal with business
* objects, query objects, and {@code org.springframework.dao} exceptions.
*
* <p>Can be used within a service implementation via direct instantiation
* with a PersistenceManagerFactory reference, or get prepared in an
* application context and given to services as bean reference.
* Note: The PersistenceManagerFactory should always be configured as bean in
* the application context, in the first case given to the service directly,
* in the second case to the prepared template.
*
* <p>This class can be considered as direct alternative to working with the
* raw JDO PersistenceManager API (through
* {@code PersistenceManagerFactoryUtils.getPersistenceManager()}).
* The major advantage is its automatic conversion to DataAccessExceptions, the
* major disadvantage that no checked application exceptions can get thrown from
* within data access code. Corresponding checks and the actual throwing of such
* exceptions can often be deferred to after callback execution, though.
*
* <p>{@link LocalPersistenceManagerFactoryBean} is the preferred way of obtaining
* a reference to a specific PersistenceManagerFactory, at least in a non-EJB
* environment. The Spring application context will manage its lifecycle,
* initializing and shutting down the factory as part of the application.
*
* <p>Note that lazy loading will just work with an open JDO PersistenceManager,
* either within a Spring-driven transaction (with JdoTransactionManager or
* JtaTransactionManager) or within OpenPersistenceManagerInViewFilter/Interceptor.
* Furthermore, some operations just make sense within transactions,
* for example: {@code evict}, {@code evictAll}, {@code flush}.
*
* <p><b>NOTE: This class requires JDO 2.0 or higher, as of Spring 2.5.</b>
* As of Spring 3.0, it follows JDO 2.1 conventions in terms of generic
* parameter and return types, which still remaining compatible with JDO 2.0.
*
* @author Juergen Hoeller
* @since 03.06.2003
* @see #setPersistenceManagerFactory
* @see JdoCallback
* @see javax.jdo.PersistenceManager
* @see LocalPersistenceManagerFactoryBean
* @see JdoTransactionManager
* @see org.springframework.transaction.jta.JtaTransactionManager
* @see org.springframework.orm.jdo.support.OpenPersistenceManagerInViewFilter
* @see org.springframework.orm.jdo.support.OpenPersistenceManagerInViewInterceptor
* @deprecated as of Spring 3.1, in favor of native PersistenceManager usage
* (see {@link TransactionAwarePersistenceManagerFactoryProxy} and
* {@link org.springframework.orm.jdo.support.SpringPersistenceManagerProxyBean})
*/
@Deprecated
public class JdoTemplate extends JdoAccessor implements JdoOperations {
private boolean allowCreate = true;
private boolean exposeNativePersistenceManager = false;
/**
* Create a new JdoTemplate instance.
*/
public JdoTemplate() {
}
/**
* Create a new JdoTemplate instance.
* @param pmf PersistenceManagerFactory to create PersistenceManagers
*/
public JdoTemplate(PersistenceManagerFactory pmf) {
setPersistenceManagerFactory(pmf);
afterPropertiesSet();
}
/**
* Create a new JdoTemplate instance.
* @param pmf PersistenceManagerFactory to create PersistenceManagers
* @param allowCreate if a non-transactional PersistenceManager should be created
* when no transactional PersistenceManager can be found for the current thread
*/
public JdoTemplate(PersistenceManagerFactory pmf, boolean allowCreate) {
setPersistenceManagerFactory(pmf);
setAllowCreate(allowCreate);
afterPropertiesSet();
}
/**
* Set if a new PersistenceManager should be created when no transactional
* PersistenceManager can be found for the current thread.
* <p>JdoTemplate is aware of a corresponding PersistenceManager bound to the
* current thread, for example when using JdoTransactionManager.
* If allowCreate is true, a new non-transactional PersistenceManager will be
* created if none found, which needs to be closed at the end of the operation.
* If false, an IllegalStateException will get thrown in this case.
* @see PersistenceManagerFactoryUtils#getPersistenceManager(javax.jdo.PersistenceManagerFactory, boolean)
*/
public void setAllowCreate(boolean allowCreate) {
this.allowCreate = allowCreate;
}
/**
* Return if a new PersistenceManager should be created if no thread-bound found.
*/
public boolean isAllowCreate() {
return this.allowCreate;
}
/**
* Set whether to expose the native JDO PersistenceManager to JdoCallback
* code. Default is "false": a PersistenceManager proxy will be returned,
* suppressing {@code close} calls and automatically applying transaction
* timeouts (if any).
* <p>As there is often a need to cast to a provider-specific PersistenceManager
* class in DAOs that use provider-specific functionality, the exposed proxy
* implements all interfaces implemented by the original PersistenceManager.
* If this is not sufficient, turn this flag to "true".
* @see JdoCallback
* @see javax.jdo.PersistenceManager
* @see #prepareQuery
*/
public void setExposeNativePersistenceManager(boolean exposeNativePersistenceManager) {
this.exposeNativePersistenceManager = exposeNativePersistenceManager;
}
/**
* Return whether to expose the native JDO PersistenceManager to JdoCallback
* code, or rather a PersistenceManager proxy.
*/
public boolean isExposeNativePersistenceManager() {
return this.exposeNativePersistenceManager;
}
public <T> T execute(JdoCallback<T> action) throws DataAccessException {
return execute(action, isExposeNativePersistenceManager());
}
public Collection executeFind(JdoCallback<?> action) throws DataAccessException {
Object result = execute(action, isExposeNativePersistenceManager());
if (result != null && !(result instanceof Collection)) {
throw new InvalidDataAccessApiUsageException(
"Result object returned from JdoCallback isn't a Collection: [" + result + "]");
}
return (Collection) result;
}
/**
* Execute the action specified by the given action object within a
* PersistenceManager.
* @param action callback object that specifies the JDO action
* @param exposeNativePersistenceManager whether to expose the native
* JDO persistence manager to callback code
* @return a result object returned by the action, or {@code null}
* @throws org.springframework.dao.DataAccessException in case of JDO errors
*/
public <T> T execute(JdoCallback<T> action, boolean exposeNativePersistenceManager) throws DataAccessException {
Assert.notNull(action, "Callback object must not be null");
PersistenceManager pm = PersistenceManagerFactoryUtils.getPersistenceManager(
getPersistenceManagerFactory(), isAllowCreate());
boolean existingTransaction =
TransactionSynchronizationManager.hasResource(getPersistenceManagerFactory());
try {
PersistenceManager pmToExpose = (exposeNativePersistenceManager ? pm : createPersistenceManagerProxy(pm));
T result = action.doInJdo(pmToExpose);
flushIfNecessary(pm, existingTransaction);
return postProcessResult(result, pm, existingTransaction);
}
catch (JDOException ex) {
throw convertJdoAccessException(ex);
}
catch (RuntimeException ex) {
// callback code threw application exception
throw ex;
}
finally {
PersistenceManagerFactoryUtils.releasePersistenceManager(pm, getPersistenceManagerFactory());
}
}
/**
* Create a close-suppressing proxy for the given JDO PersistenceManager.
* Called by the {@code execute} method.
* <p>The proxy also prepares returned JDO Query objects.
* @param pm the JDO PersistenceManager to create a proxy for
* @return the PersistenceManager proxy, implementing all interfaces
* implemented by the passed-in PersistenceManager object (that is,
* also implementing all provider-specific extension interfaces)
* @see javax.jdo.PersistenceManager#close()
* @see #execute(JdoCallback, boolean)
* @see #prepareQuery
*/
protected PersistenceManager createPersistenceManagerProxy(PersistenceManager pm) {
Class[] ifcs = ClassUtils.getAllInterfacesForClass(pm.getClass(), getClass().getClassLoader());
return (PersistenceManager) Proxy.newProxyInstance(
pm.getClass().getClassLoader(), ifcs, new CloseSuppressingInvocationHandler(pm));
}
/**
* Post-process the given result object, which might be a Collection.
* Called by the {@code execute} method.
* <p>Default implementation always returns the passed-in Object as-is.
* Subclasses might override this to automatically detach result
* collections or even single result objects.
* @param pm the current JDO PersistenceManager
* @param result the result object (might be a Collection)
* @param existingTransaction if executing within an existing transaction
* (within an existing JDO PersistenceManager that won't be closed immediately)
* @return the post-processed result object (can be simply be the passed-in object)
* @see #execute(JdoCallback, boolean)
*/
protected <T> T postProcessResult(T result, PersistenceManager pm, boolean existingTransaction) {
return result;
}
//-------------------------------------------------------------------------
// Convenience methods for load, save, delete
//-------------------------------------------------------------------------
public Object getObjectById(final Object objectId) throws DataAccessException {
return execute(new JdoCallback<Object>() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
return pm.getObjectById(objectId, true);
}
}, true);
}
public <T> T getObjectById(final Class<T> entityClass, final Object idValue) throws DataAccessException {
return execute(new JdoCallback<T>() {
public T doInJdo(PersistenceManager pm) throws JDOException {
return pm.getObjectById(entityClass, idValue);
}
}, true);
}
public void evict(final Object entity) throws DataAccessException {
execute(new JdoCallback<Object>() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
pm.evict(entity);
return null;
}
}, true);
}
public void evictAll(final Collection entities) throws DataAccessException {
execute(new JdoCallback<Object>() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
pm.evictAll(entities);
return null;
}
}, true);
}
public void evictAll() throws DataAccessException {
execute(new JdoCallback<Object>() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
pm.evictAll();
return null;
}
}, true);
}
public void refresh(final Object entity) throws DataAccessException {
execute(new JdoCallback<Object>() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
pm.refresh(entity);
return null;
}
}, true);
}
public void refreshAll(final Collection entities) throws DataAccessException {
execute(new JdoCallback<Object>() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
pm.refreshAll(entities);
return null;
}
}, true);
}
public void refreshAll() throws DataAccessException {
execute(new JdoCallback<Object>() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
pm.refreshAll();
return null;
}
}, true);
}
public <T> T makePersistent(final T entity) throws DataAccessException {
return execute(new JdoCallback<T>() {
public T doInJdo(PersistenceManager pm) throws JDOException {
return pm.makePersistent(entity);
}
}, true);
}
public <T> Collection<T> makePersistentAll(final Collection<T> entities) throws DataAccessException {
return execute(new JdoCallback<Collection<T>>() {
public Collection<T> doInJdo(PersistenceManager pm) throws JDOException {
return pm.makePersistentAll(entities);
}
}, true);
}
public void deletePersistent(final Object entity) throws DataAccessException {
execute(new JdoCallback<Object>() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
pm.deletePersistent(entity);
return null;
}
}, true);
}
public void deletePersistentAll(final Collection entities) throws DataAccessException {
execute(new JdoCallback<Object>() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
pm.deletePersistentAll(entities);
return null;
}
}, true);
}
public <T> T detachCopy(final T entity) {
return execute(new JdoCallback<T>() {
public T doInJdo(PersistenceManager pm) throws JDOException {
return pm.detachCopy(entity);
}
}, true);
}
public <T> Collection<T> detachCopyAll(final Collection<T> entities) {
return execute(new JdoCallback<Collection<T>>() {
public Collection<T> doInJdo(PersistenceManager pm) throws JDOException {
return pm.detachCopyAll(entities);
}
}, true);
}
public void flush() throws DataAccessException {
execute(new JdoCallback<Object>() {
public Object doInJdo(PersistenceManager pm) throws JDOException {
pm.flush();
return null;
}
}, true);
}
//-------------------------------------------------------------------------
// Convenience finder methods
//-------------------------------------------------------------------------
public <T> Collection<T> find(Class<T> entityClass) throws DataAccessException {
return find(entityClass, null, null);
}
public <T> Collection<T> find(Class<T> entityClass, String filter) throws DataAccessException {
return find(entityClass, filter, null);
}
public <T> Collection<T> find(final Class<T> entityClass, final String filter, final String ordering)
throws DataAccessException {
return execute(new JdoCallback<Collection<T>>() {
@SuppressWarnings("unchecked")
public Collection<T> doInJdo(PersistenceManager pm) throws JDOException {
Query query = (filter != null ? pm.newQuery(entityClass, filter) : pm.newQuery(entityClass));
prepareQuery(query);
if (ordering != null) {
query.setOrdering(ordering);
}
return (Collection<T>) query.execute();
}
}, true);
}
public <T> Collection<T> find(Class<T> entityClass, String filter, String parameters, Object... values)
throws DataAccessException {
return find(entityClass, filter, parameters, values, null);
}
public <T> Collection<T> find(
final Class<T> entityClass, final String filter, final String parameters, final Object[] values,
final String ordering) throws DataAccessException {
return execute(new JdoCallback<Collection<T>>() {
@SuppressWarnings("unchecked")
public Collection<T> doInJdo(PersistenceManager pm) throws JDOException {
Query query = pm.newQuery(entityClass, filter);
prepareQuery(query);
query.declareParameters(parameters);
if (ordering != null) {
query.setOrdering(ordering);
}
return (Collection<T>) query.executeWithArray(values);
}
}, true);
}
public <T> Collection<T> find(
Class<T> entityClass, String filter, String parameters, Map<String, ?> values)
throws DataAccessException {
return find(entityClass, filter, parameters, values, null);
}
public <T> Collection<T> find(
final Class<T> entityClass, final String filter, final String parameters,
final Map<String, ?> values, final String ordering) throws DataAccessException {
return execute(new JdoCallback<Collection<T>>() {
@SuppressWarnings("unchecked")
public Collection<T> doInJdo(PersistenceManager pm) throws JDOException {
Query query = pm.newQuery(entityClass, filter);
prepareQuery(query);
query.declareParameters(parameters);
if (ordering != null) {
query.setOrdering(ordering);
}
return (Collection<T>) query.executeWithMap(values);
}
}, true);
}
public Collection find(final String language, final Object queryObject) throws DataAccessException {
return execute(new JdoCallback<Collection>() {
public Collection doInJdo(PersistenceManager pm) throws JDOException {
Query query = pm.newQuery(language, queryObject);
prepareQuery(query);
return (Collection) query.execute();
}
}, true);
}
public Collection find(final String queryString) throws DataAccessException {
return execute(new JdoCallback<Collection>() {
public Collection doInJdo(PersistenceManager pm) throws JDOException {
Query query = pm.newQuery(queryString);
prepareQuery(query);
return (Collection) query.execute();
}
}, true);
}
public Collection find(final String queryString, final Object... values) throws DataAccessException {
return execute(new JdoCallback<Collection>() {
public Collection doInJdo(PersistenceManager pm) throws JDOException {
Query query = pm.newQuery(queryString);
prepareQuery(query);
return (Collection) query.executeWithArray(values);
}
}, true);
}
public Collection find(final String queryString, final Map<String, ?> values) throws DataAccessException {
return execute(new JdoCallback<Collection>() {
public Collection doInJdo(PersistenceManager pm) throws JDOException {
Query query = pm.newQuery(queryString);
prepareQuery(query);
return (Collection) query.executeWithMap(values);
}
}, true);
}
public <T> Collection<T> findByNamedQuery(final Class<T> entityClass, final String queryName)
throws DataAccessException {
return execute(new JdoCallback<Collection<T>>() {
@SuppressWarnings("unchecked")
public Collection<T> doInJdo(PersistenceManager pm) throws JDOException {
Query query = pm.newNamedQuery(entityClass, queryName);
prepareQuery(query);
return (Collection<T>) query.execute();
}
}, true);
}
public <T> Collection<T> findByNamedQuery(final Class<T> entityClass, final String queryName, final Object... values)
throws DataAccessException {
return execute(new JdoCallback<Collection<T>>() {
@SuppressWarnings("unchecked")
public Collection<T> doInJdo(PersistenceManager pm) throws JDOException {
Query query = pm.newNamedQuery(entityClass, queryName);
prepareQuery(query);
return (Collection<T>) query.executeWithArray(values);
}
}, true);
}
public <T> Collection<T> findByNamedQuery(
final Class<T> entityClass, final String queryName, final Map<String, ?> values)
throws DataAccessException {
return execute(new JdoCallback<Collection<T>>() {
@SuppressWarnings("unchecked")
public Collection<T> doInJdo(PersistenceManager pm) throws JDOException {
Query query = pm.newNamedQuery(entityClass, queryName);
prepareQuery(query);
return (Collection<T>) query.executeWithMap(values);
}
}, true);
}
/**
* Prepare the given JDO query object. To be used within a JdoCallback.
* <p>Applies a transaction timeout, if any. If you don't use such timeouts,
* the call is a no-op.
* <p>In general, prefer a proxied PersistenceManager instead, which will
* automatically apply the transaction timeout (through the use of a special
* PersistenceManager proxy). You need to set the "exposeNativePersistenceManager"
* property to "false" to activate this. Note that you won't be able to cast
* to a provider-specific JDO PersistenceManager class anymore then.
* @param query the JDO query object
* @throws JDOException if the query could not be properly prepared
* @see JdoCallback#doInJdo
* @see PersistenceManagerFactoryUtils#applyTransactionTimeout
* @see #setExposeNativePersistenceManager
*/
public void prepareQuery(Query query) throws JDOException {
PersistenceManagerFactoryUtils.applyTransactionTimeout(
query, getPersistenceManagerFactory(), getJdoDialect());
}
/**
* Invocation handler that suppresses close calls on JDO PersistenceManagers.
* Also prepares returned Query objects.
* @see javax.jdo.PersistenceManager#close()
*/
private class CloseSuppressingInvocationHandler implements InvocationHandler {
private final PersistenceManager target;
public CloseSuppressingInvocationHandler(PersistenceManager target) {
this.target = target;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// Invocation on PersistenceManager interface (or provider-specific extension) coming in...
if (method.getName().equals("equals")) {
// Only consider equal when proxies are identical.
return (proxy == args[0]);
}
else if (method.getName().equals("hashCode")) {
// Use hashCode of PersistenceManager proxy.
return System.identityHashCode(proxy);
}
else if (method.getName().equals("close")) {
// Handle close method: suppress, not valid.
return null;
}
// Invoke method on target PersistenceManager.
try {
Object retVal = method.invoke(this.target, args);
// If return value is a JDO Query object, apply transaction timeout.
if (retVal instanceof Query) {
prepareQuery(((Query) retVal));
}
return retVal;
}
catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -40,12 +40,12 @@ import org.springframework.transaction.support.ResourceTransactionManager;
import org.springframework.transaction.support.TransactionSynchronizationManager;
/**
* {@link org.springframework.transaction.PlatformTransactionManager} implementation
* for a single JDO {@link javax.jdo.PersistenceManagerFactory}. Binds a JDO
* PersistenceManager from the specified factory to the thread, potentially allowing
* for one thread-bound PersistenceManager per factory.
* {@link PersistenceManagerFactoryUtils} and {@link JdoTemplate} are aware of
* thread-bound persistence managers and participate in such transactions automatically.
* {@link org.springframework.transaction.PlatformTransactionManager} implementation for a
* single JDO {@link javax.jdo.PersistenceManagerFactory}. Binds a JDO PersistenceManager
* from the specified factory to the thread, potentially allowing for one thread-bound
* PersistenceManager per factory. {@link PersistenceManagerFactoryUtils} and
* {@link org.springframework.orm.jdo.support.SpringPersistenceManagerProxyBean} are aware
* of thread-bound persistence managers and participate in such transactions automatically.
* Using either of those (or going through a {@link TransactionAwarePersistenceManagerFactoryProxy}
* is required for JDO access code supporting this transaction management mechanism.
*
@ -91,7 +91,6 @@ import org.springframework.transaction.support.TransactionSynchronizationManager
* @see LocalPersistenceManagerFactoryBean
* @see PersistenceManagerFactoryUtils#getPersistenceManager
* @see PersistenceManagerFactoryUtils#releasePersistenceManager
* @see JdoTemplate
* @see TransactionAwarePersistenceManagerFactoryProxy
* @see org.springframework.jdbc.datasource.DataSourceUtils#getConnection
* @see org.springframework.jdbc.datasource.DataSourceUtils#releaseConnection
@ -418,7 +417,7 @@ public class JdoTransactionManager extends AbstractPlatformTransactionManager
}
/**
* This implementation returns "true": a JDO2 commit will properly handle
* This implementation returns "true": a JDO commit will properly handle
* transactions that have been marked rollback-only at a global level.
*/
@Override

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -45,16 +45,16 @@ import org.springframework.util.CollectionUtils;
* dependency injection. Note that switching to a JNDI lookup or to a bean-style
* PersistenceManagerFactory instance is just a matter of configuration!
*
* <p><b>NOTE: This class requires JDO 2.0 or higher, as of Spring 2.5.</b>
* Since JDO 2.1, it will also expose the JPA {@link javax.persistence.EntityManagerFactory}
* as long as the JDO provider creates a {@link javax.jdo.JDOEntityManagerFactory} reference
* <p><b>NOTE: This class requires JDO 3.0 or higher, as of Spring 4.0.</b>
* It will also expose the JPA {@link javax.persistence.EntityManagerFactory} as long
* as the JDO provider creates a {@link javax.jdo.JDOEntityManagerFactory} reference
* underneath, which means that this class can be used as a replacement for
* {@link org.springframework.orm.jpa.LocalEntityManagerFactoryBean} in such a scenario.
*
* <p>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",
* You may alternatively specify a "persistenceManagerFactoryName",
* referring to a PMF definition in "META-INF/jdoconfig.xml"
* (see {@link #setPersistenceManagerFactoryName}).
*
@ -100,7 +100,6 @@ import org.springframework.util.CollectionUtils;
*
* @author Juergen Hoeller
* @since 03.06.2003
* @see JdoTemplate#setPersistenceManagerFactory
* @see JdoTransactionManager#setPersistenceManagerFactory
* @see org.springframework.jndi.JndiObjectFactoryBean
* @see javax.jdo.JDOHelper#getPersistenceManagerFactory
@ -128,10 +127,10 @@ public class LocalPersistenceManagerFactoryBean implements FactoryBean<Persisten
/**
* Specify the name of the desired PersistenceManagerFactory.
* <p>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),
* <p>This may either be a properties resource in the classpath if such a resource
* exists, or a PMF definition with that name from "META-INF/jdoconfig.xml",
* 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).
* persistence-unit name from "META-INF/persistence.xml" (JPA).
* <p>Default is none: Either 'persistenceManagerFactoryName' or 'configLocation'
* or 'jdoProperties' needs to be specified.
* @see #setConfigLocation

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -43,12 +43,12 @@ import org.springframework.transaction.support.TransactionSynchronizationManager
import org.springframework.util.Assert;
/**
* Helper class featuring methods for JDO PersistenceManager handling,
* Helper class featuring methods for JDO {@link PersistenceManager} handling,
* allowing for reuse of PersistenceManager instances within transactions.
* Also provides support for exception translation.
*
* <p>Used internally by {@link JdoTemplate}, {@link JdoInterceptor} and
* {@link JdoTransactionManager}. Can also be used directly in application code.
* <p>Used internally by {@link JdoTransactionManager}.
* Can also be used directly in application code.
*
* @author Juergen Hoeller
* @since 03.06.2003
@ -194,32 +194,28 @@ public abstract class PersistenceManagerFactoryUtils {
* Apply the current transaction timeout, if any, to the given JDO Query object.
* @param query the JDO Query object
* @param pmf JDO PersistenceManagerFactory that the Query was created for
* @param jdoDialect the JdoDialect to use for applying a query timeout
* (must not be {@code null})
* @throws JDOException if thrown by JDO methods
* @see JdoDialect#applyQueryTimeout
*/
public static void applyTransactionTimeout(
Query query, PersistenceManagerFactory pmf, JdoDialect jdoDialect) throws JDOException {
public static void applyTransactionTimeout(Query query, PersistenceManagerFactory pmf) throws JDOException {
Assert.notNull(query, "No Query object specified");
PersistenceManagerHolder pmHolder =
(PersistenceManagerHolder) TransactionSynchronizationManager.getResource(pmf);
if (pmHolder != null && pmHolder.hasTimeout()) {
jdoDialect.applyQueryTimeout(query, pmHolder.getTimeToLiveInSeconds());
(PersistenceManagerHolder) TransactionSynchronizationManager.getResource(pmf);
if (pmHolder != null && pmHolder.hasTimeout() &&
pmf.supportedOptions().contains("javax.jdo.option.DatastoreTimeout")) {
int timeout = (int) pmHolder.getTimeToLiveInMillis();
query.setDatastoreReadTimeoutMillis(timeout);
query.setDatastoreWriteTimeoutMillis(timeout);
}
}
/**
* Convert the given JDOException to an appropriate exception from the
* {@code org.springframework.dao} hierarchy.
* <p>The most important cases like object not found or optimistic locking
* failure are covered here. For more fine-granular conversion, JdoAccessor and
* JdoTransactionManager support sophisticated translation of exceptions via a
* JdoDialect.
* <p>The most important cases like object not found or optimistic locking failure
* are covered here. For more fine-granular conversion, JdoTransactionManager
* supports sophisticated translation of exceptions via a JdoDialect.
* @param ex JDOException that occured
* @return the corresponding DataAccessException instance
* @see JdoAccessor#convertJdoAccessException
* @see JdoTransactionManager#convertJdoAccessException
* @see JdoDialect#translateException
*/

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -50,12 +50,6 @@ import org.springframework.util.ClassUtils;
* receiving the reference through Dependency Injection. This will work without
* any Spring API dependencies in the DAO code!
*
* <p>It is usually preferable to write your JDO-based DAOs with Spring's
* {@link JdoTemplate}, offering benefits such as consistent data access
* exceptions instead of JDOExceptions at the DAO layer. However, Spring's
* resource and transaction management (and Dependency Injection) will work
* for DAOs written against the plain JDO API as well.
*
* <p>Of course, you can still access the target PersistenceManagerFactory
* even when your DAOs go through this proxy, by defining a bean reference
* that points directly at your target PersistenceManagerFactory bean.

View File

@ -1,175 +0,0 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.orm.jdo.support;
import javax.jdo.JDOException;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.dao.support.DaoSupport;
import org.springframework.orm.jdo.JdoTemplate;
import org.springframework.orm.jdo.PersistenceManagerFactoryUtils;
/**
* Convenient super class for JDO data access objects.
*
* <p>Requires a PersistenceManagerFactory to be set, providing a JdoTemplate
* based on it to subclasses. Can alternatively be initialized directly with a
* JdoTemplate, to reuse the latter's settings such as the PersistenceManagerFactory,
* JdoDialect, flush mode, etc.
*
* <p>This base class is mainly intended for JdoTemplate usage but can also
* be used when working with PersistenceManagerFactoryUtils directly, for example
* in combination with JdoInterceptor-managed PersistenceManagers. Convenience
* {@code getPersistenceManager} and {@code releasePersistenceManager}
* methods are provided for that usage style.
*
* <p>This class will create its own JdoTemplate if only a PersistenceManagerFactory
* is passed in. The "allowCreate" flag on that JdoTemplate will be "true" by default.
* A custom JdoTemplate instance can be used through overriding {@code createJdoTemplate}.
*
* @author Juergen Hoeller
* @since 28.07.2003
* @see #setPersistenceManagerFactory
* @see #setJdoTemplate
* @see #createJdoTemplate
* @see #getPersistenceManager
* @see #releasePersistenceManager
* @see org.springframework.orm.jdo.JdoTemplate
* @see org.springframework.orm.jdo.JdoInterceptor
* @deprecated as of Spring 3.1, in favor of native PersistenceManager usage
* (see {@link org.springframework.orm.jdo.TransactionAwarePersistenceManagerFactoryProxy}
* and {@link SpringPersistenceManagerProxyBean})
*/
@Deprecated
public abstract class JdoDaoSupport extends DaoSupport {
private JdoTemplate jdoTemplate;
/**
* Set the JDO PersistenceManagerFactory to be used by this DAO.
* Will automatically create a JdoTemplate for the given PersistenceManagerFactory.
* @see #createJdoTemplate
* @see #setJdoTemplate
*/
public final void setPersistenceManagerFactory(PersistenceManagerFactory persistenceManagerFactory) {
if (this.jdoTemplate == null || persistenceManagerFactory != this.jdoTemplate.getPersistenceManagerFactory()) {
this.jdoTemplate = createJdoTemplate(persistenceManagerFactory);
}
}
/**
* Create a JdoTemplate for the given PersistenceManagerFactory.
* Only invoked if populating the DAO with a PersistenceManagerFactory reference!
* <p>Can be overridden in subclasses to provide a JdoTemplate instance
* with different configuration, or a custom JdoTemplate subclass.
* @param persistenceManagerFactory the JDO PersistenceManagerFactoryto create a JdoTemplate for
* @return the new JdoTemplate instance
* @see #setPersistenceManagerFactory
*/
protected JdoTemplate createJdoTemplate(PersistenceManagerFactory persistenceManagerFactory) {
return new JdoTemplate(persistenceManagerFactory);
}
/**
* Return the JDO PersistenceManagerFactory used by this DAO.
*/
public final PersistenceManagerFactory getPersistenceManagerFactory() {
return (this.jdoTemplate != null ? this.jdoTemplate.getPersistenceManagerFactory() : null);
}
/**
* Set the JdoTemplate for this DAO explicitly,
* as an alternative to specifying a PersistenceManagerFactory.
* @see #setPersistenceManagerFactory
*/
public final void setJdoTemplate(JdoTemplate jdoTemplate) {
this.jdoTemplate = jdoTemplate;
}
/**
* Return the JdoTemplate for this DAO, pre-initialized
* with the PersistenceManagerFactory or set explicitly.
*/
public final JdoTemplate getJdoTemplate() {
return jdoTemplate;
}
@Override
protected final void checkDaoConfig() {
if (this.jdoTemplate == null) {
throw new IllegalArgumentException("persistenceManagerFactory or jdoTemplate is required");
}
}
/**
* Get a JDO PersistenceManager, either from the current transaction or
* a new one. The latter is only allowed if the "allowCreate" setting
* of this bean's JdoTemplate is true.
* @return the JDO PersistenceManager
* @throws DataAccessResourceFailureException if the PersistenceManager couldn't be created
* @throws IllegalStateException if no thread-bound PersistenceManager found and allowCreate false
* @see org.springframework.orm.jdo.PersistenceManagerFactoryUtils#getPersistenceManager
*/
protected final PersistenceManager getPersistenceManager() {
return getPersistenceManager(this.jdoTemplate.isAllowCreate());
}
/**
* Get a JDO PersistenceManager, either from the current transaction or
* a new one. The latter is only allowed if "allowCreate" is true.
* @param allowCreate if a non-transactional PersistenceManager should be created
* when no transactional PersistenceManager can be found for the current thread
* @return the JDO PersistenceManager
* @throws DataAccessResourceFailureException if the PersistenceManager couldn't be created
* @throws IllegalStateException if no thread-bound PersistenceManager found and allowCreate false
* @see org.springframework.orm.jdo.PersistenceManagerFactoryUtils#getPersistenceManager
*/
protected final PersistenceManager getPersistenceManager(boolean allowCreate)
throws DataAccessResourceFailureException, IllegalStateException {
return PersistenceManagerFactoryUtils.getPersistenceManager(getPersistenceManagerFactory(), allowCreate);
}
/**
* Convert the given JDOException to an appropriate exception from the
* org.springframework.dao hierarchy.
* <p>Delegates to the convertJdoAccessException method of this DAO's JdoTemplate.
* @param ex JDOException that occured
* @return the corresponding DataAccessException instance
* @see #setJdoTemplate
* @see org.springframework.orm.jdo.JdoTemplate#convertJdoAccessException
*/
protected final DataAccessException convertJdoAccessException(JDOException ex) {
return this.jdoTemplate.convertJdoAccessException(ex);
}
/**
* Close the given JDO PersistenceManager, created via this DAO's
* PersistenceManagerFactory, if it isn't bound to the thread.
* @param pm PersistenceManager to close
* @see org.springframework.orm.jdo.PersistenceManagerFactoryUtils#releasePersistenceManager
*/
protected final void releasePersistenceManager(PersistenceManager pm) {
PersistenceManagerFactoryUtils.releasePersistenceManager(pm, getPersistenceManagerFactory());
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -17,7 +17,6 @@
package org.springframework.orm.jdo.support;
import java.io.IOException;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.servlet.FilterChain;
@ -53,7 +52,6 @@ import org.springframework.web.filter.OncePerRequestFilter;
* @author Juergen Hoeller
* @since 1.1
* @see OpenPersistenceManagerInViewInterceptor
* @see org.springframework.orm.jdo.JdoInterceptor
* @see org.springframework.orm.jdo.JdoTransactionManager
* @see org.springframework.orm.jdo.PersistenceManagerFactoryUtils#getPersistenceManager
* @see org.springframework.transaction.support.TransactionSynchronizationManager

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -44,14 +44,11 @@ import org.springframework.web.context.request.WebRequestInterceptor;
*
* <p>In contrast to {@link OpenPersistenceManagerInViewFilter}, this interceptor
* is set up in a Spring application context and can thus take advantage of
* bean wiring. It inherits common JDO configuration properties from
* {@link org.springframework.orm.jdo.JdoAccessor}, to be configured in a
* bean definition.
* bean wiring.
*
* @author Juergen Hoeller
* @since 1.1
* @see OpenPersistenceManagerInViewFilter
* @see org.springframework.orm.jdo.JdoInterceptor
* @see org.springframework.orm.jdo.JdoTransactionManager
* @see org.springframework.orm.jdo.PersistenceManagerFactoryUtils#getPersistenceManager
* @see org.springframework.transaction.support.TransactionSynchronizationManager

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -39,19 +39,16 @@ import org.springframework.util.Assert;
* Spring-style PersistenceManager proxy available for bean references.
*
* <p>The main advantage of this proxy is that it allows DAOs to work with a
* plain JDO PersistenceManager reference in JDO 2.1 style
* plain JDO PersistenceManager reference in JDO 3.0 style
* (see {@link javax.jdo.PersistenceManagerFactory#getPersistenceManagerProxy()}),
* while still participating in Spring's resource and transaction management.
*
* <p>The behavior of this proxy matches the behavior that the JDO 2.1 spec
* <p>The behavior of this proxy matches the behavior that the JDO 3.0 spec
* defines for a PersistenceManager proxy. Hence, DAOs could seamlessly switch
* between {@link StandardPersistenceManagerProxyBean} and this Spring-style proxy,
* receiving the reference through Dependency Injection. This will work without
* any Spring API dependencies in the DAO code!
*
* <p>Note: In contrast to {@link StandardPersistenceManagerProxyBean}, this proxy
* works with JDO 2.0 and higher. It does not require JDO 2.1.
*
* @author Juergen Hoeller
* @since 3.0
* @see StandardPersistenceManagerProxyBean
@ -212,7 +209,7 @@ public class SpringPersistenceManagerProxyBean implements FactoryBean<Persistenc
Object retVal = method.invoke(pm, args);
if (retVal instanceof Query) {
PersistenceManagerFactoryUtils.applyTransactionTimeout(
(Query) retVal, getPersistenceManagerFactory(), getJdoDialect());
(Query) retVal, getPersistenceManagerFactory());
}
return retVal;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -25,16 +25,14 @@ import org.springframework.util.Assert;
/**
* Proxy that implements the {@link javax.jdo.PersistenceManager} interface,
* delegating to a thread-bound PersistenceManager on each invocation -
* as defined by the JDO 2.1 specification. This class makes such a standard
* as defined by the JDO 3.0 specification. This class makes such a standard
* JDO PersistenceManager proxy available for bean references.
*
* <p>The main advantage of this proxy is that it allows DAOs to work with a
* plain JDO PersistenceManager reference in JDO 2.1 style
* plain JDO PersistenceManager reference in JDO 3.0 style
* (see {@link javax.jdo.PersistenceManagerFactory#getPersistenceManagerProxy()}),
* exposing the exact behavior that the target JDO provider implements.
*
* <p>Note: This proxy requires JDO 2.1 or higher.
*
* @author Juergen Hoeller
* @since 3.0
* @see SpringPersistenceManagerProxyBean

View File

@ -1,7 +1,6 @@
/**
*
* Classes supporting the {@code org.springframework.orm.jdo} package.
* Contains a DAO base class for JdoTemplate usage.
*
*/
package org.springframework.orm.jdo.support;

View File

@ -1,150 +0,0 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.orm.jdo;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import org.aopalliance.intercept.Interceptor;
import org.aopalliance.intercept.Invocation;
import org.aopalliance.intercept.MethodInvocation;
import org.junit.Test;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
/**
* @author Juergen Hoeller
* @author Phillip Webb
*/
public class JdoInterceptorTests {
@Test
public void testInterceptor() {
PersistenceManagerFactory pmf = mock(PersistenceManagerFactory.class);
PersistenceManager pm = mock(PersistenceManager.class);
given(pmf.getPersistenceManager()).willReturn(pm);
JdoInterceptor interceptor = new JdoInterceptor();
interceptor.setPersistenceManagerFactory(pmf);
try {
interceptor.invoke(new TestInvocation(pmf));
}
catch (Throwable t) {
fail("Should not have thrown Throwable: " + t.getMessage());
}
verify(pm).close();
}
@Test
public void testInterceptorWithPrebound() {
PersistenceManagerFactory pmf = mock(PersistenceManagerFactory.class);
PersistenceManager pm = mock(PersistenceManager.class);
TransactionSynchronizationManager.bindResource(pmf, new PersistenceManagerHolder(pm));
JdoInterceptor interceptor = new JdoInterceptor();
interceptor.setPersistenceManagerFactory(pmf);
try {
interceptor.invoke(new TestInvocation(pmf));
}
catch (Throwable t) {
fail("Should not have thrown Throwable: " + t.getMessage());
}
finally {
TransactionSynchronizationManager.unbindResource(pmf);
}
}
@SuppressWarnings("unused")
private static class TestInvocation implements MethodInvocation {
private PersistenceManagerFactory persistenceManagerFactory;
public TestInvocation(PersistenceManagerFactory persistenceManagerFactory) {
this.persistenceManagerFactory = persistenceManagerFactory;
}
@Override
public Object proceed() throws Throwable {
if (!TransactionSynchronizationManager.hasResource(this.persistenceManagerFactory)) {
throw new IllegalStateException("PersistenceManager not bound");
}
return null;
}
@Override
public Object[] getArguments() {
return null;
}
public int getCurrentInterceptorIndex() {
return 0;
}
public int getNumberOfInterceptors() {
return 0;
}
public Interceptor getInterceptor(int i) {
return null;
}
@Override
public Method getMethod() {
return null;
}
@Override
public AccessibleObject getStaticPart() {
return getMethod();
}
public Object getArgument(int i) {
return null;
}
public void setArgument(int i, Object handler) {
}
public int getArgumentCount() {
return 0;
}
@Override
public Object getThis() {
return null;
}
public Object getProxy() {
return null;
}
public Invocation cloneInstance() {
return null;
}
public void release() {
}
}
}

View File

@ -1,534 +0,0 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.orm.jdo;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import javax.jdo.JDODataStoreException;
import javax.jdo.JDOException;
import javax.jdo.JDOFatalDataStoreException;
import javax.jdo.JDOFatalUserException;
import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.JDOOptimisticVerificationException;
import javax.jdo.JDOUserException;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;
import org.junit.Before;
import org.junit.Test;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
/**
* @author Juergen Hoeller
* @author Phillip Webb
* @since 03.06.2003
*/
public class JdoTemplateTests {
private PersistenceManagerFactory pmf;
private PersistenceManager pm;
@Before
public void setUp() {
pmf = mock(PersistenceManagerFactory.class);
pm = mock(PersistenceManager.class);
}
@Test
public void testTemplateExecuteWithNotAllowCreate() {
JdoTemplate jt = new JdoTemplate();
jt.setPersistenceManagerFactory(pmf);
jt.setAllowCreate(false);
try {
jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm) {
return null;
}
});
fail("Should have thrown IllegalStateException");
}
catch (IllegalStateException ex) {
// expected
}
}
@Test
public void testTemplateExecuteWithNotAllowCreateAndThreadBound() {
JdoTemplate jt = new JdoTemplate(pmf);
jt.setAllowCreate(false);
TransactionSynchronizationManager.bindResource(pmf, new PersistenceManagerHolder(pm));
final List l = new ArrayList();
l.add("test");
List result = (List) jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm) {
return l;
}
});
assertTrue("Correct result list", result == l);
TransactionSynchronizationManager.unbindResource(pmf);
}
@Test
public void testTemplateExecuteWithNewPersistenceManager() {
given(pmf.getPersistenceManager()).willReturn(pm);
JdoTemplate jt = new JdoTemplate(pmf);
final List l = new ArrayList();
l.add("test");
List result = (List) jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm) {
return l;
}
});
assertTrue("Correct result list", result == l);
verify(pm).close();
}
@Test
public void testTemplateExecuteWithThreadBoundAndFlushEager() {
JdoTemplate jt = new JdoTemplate(pmf);
jt.setFlushEager(true);
jt.setAllowCreate(false);
TransactionSynchronizationManager.bindResource(pmf, new PersistenceManagerHolder(pm));
final List l = new ArrayList();
l.add("test");
List result = (List) jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm) {
return l;
}
});
assertTrue("Correct result list", result == l);
TransactionSynchronizationManager.unbindResource(pmf);
verify(pm).flush();
}
@Test
public void testGetObjectById() {
given(pmf.getPersistenceManager()).willReturn(pm);
given(pm.getObjectById("0", true)).willReturn("A");
JdoTemplate jt = new JdoTemplate(pmf);
assertEquals("A", jt.getObjectById("0"));
verify(pm).close();
}
@Test
public void testGetObjectByIdWithClassAndValue() {
given(pmf.getPersistenceManager()).willReturn(pm);
given(pm.getObjectById(String.class, "0")).willReturn("A");
JdoTemplate jt = new JdoTemplate(pmf);
assertEquals("A", jt.getObjectById(String.class, "0"));
verify(pm).close();
}
@Test
public void testEvict() {
given(pmf.getPersistenceManager()).willReturn(pm);
JdoTemplate jt = new JdoTemplate(pmf);
jt.evict("0");
verify(pm).evict("0");
verify(pm).close();
}
@Test
public void testEvictAllWithCollection() {
Collection coll = new HashSet();
given(pmf.getPersistenceManager()).willReturn(pm);
JdoTemplate jt = new JdoTemplate(pmf);
jt.evictAll(coll);
verify(pm).evictAll(coll);
verify(pm).close();
}
@Test
public void testEvictAll() {
given(pmf.getPersistenceManager()).willReturn(pm);
JdoTemplate jt = new JdoTemplate(pmf);
jt.evictAll();
verify(pm).evictAll();
verify(pm).close();
}
@Test
public void testRefresh() {
given(pmf.getPersistenceManager()).willReturn(pm);
JdoTemplate jt = new JdoTemplate(pmf);
jt.refresh("0");
verify(pm).refresh("0");
verify(pm).close();
}
@Test
public void testRefreshAllWithCollection() {
Collection coll = new HashSet();
given(pmf.getPersistenceManager()).willReturn(pm);
JdoTemplate jt = new JdoTemplate(pmf);
jt.refreshAll(coll);
verify(pm).refreshAll(coll);
verify(pm).close();
}
@Test
public void testRefreshAll() {
given(pmf.getPersistenceManager()).willReturn(pm);
JdoTemplate jt = new JdoTemplate(pmf);
jt.refreshAll();
verify(pm).refreshAll();
verify(pm).close();
}
@Test
public void testMakePersistent() {
given(pmf.getPersistenceManager()).willReturn(pm);
JdoTemplate jt = new JdoTemplate(pmf);
jt.makePersistent("0");
verify(pm).makePersistent("0");
verify(pm).close();
}
@Test
public void testMakePersistentAll() {
Collection coll = new HashSet();
given(pmf.getPersistenceManager()).willReturn(pm);
JdoTemplate jt = new JdoTemplate(pmf);
jt.makePersistentAll(coll);
verify(pm).makePersistentAll(coll);
verify(pm).close();
}
@Test
public void testDeletePersistent() {
given(pmf.getPersistenceManager()).willReturn(pm);
JdoTemplate jt = new JdoTemplate(pmf);
jt.deletePersistent("0");
verify(pm).deletePersistent("0");
verify(pm).close();
}
@Test
public void testDeletePersistentAll() {
Collection coll = new HashSet();
given(pmf.getPersistenceManager()).willReturn(pm);
JdoTemplate jt = new JdoTemplate(pmf);
jt.deletePersistentAll(coll);
verify(pm).deletePersistentAll(coll);
verify(pm).close();
}
@Test
public void testDetachCopy() {
given(pmf.getPersistenceManager()).willReturn(pm);
given(pm.detachCopy("0")).willReturn("0x");
JdoTemplate jt = new JdoTemplate(pmf);
assertEquals("0x", jt.detachCopy("0"));
verify(pm).close();
}
@Test
public void testDetachCopyAll() {
Collection attached = new HashSet();
Collection detached = new HashSet();
given(pmf.getPersistenceManager()).willReturn(pm);
given(pm.detachCopyAll(attached)).willReturn(detached);
JdoTemplate jt = new JdoTemplate(pmf);
assertEquals(detached, jt.detachCopyAll(attached));
verify(pm).close();
}
@Test
public void testFlush() {
given(pmf.getPersistenceManager()).willReturn(pm);
JdoTemplate jt = new JdoTemplate(pmf);
jt.flush();
verify(pm).flush();
verify(pm).close();
}
@Test
public void testFlushWithDialect() {
given(pmf.getPersistenceManager()).willReturn(pm);
JdoTemplate jt = new JdoTemplate(pmf);
jt.flush();
verify(pm).flush();
verify(pm).close();
}
@Test
public void testFind() {
Query query = mock(Query.class);
given(pmf.getPersistenceManager()).willReturn(pm);
given(pm.newQuery(String.class)).willReturn(query);
Collection coll = new HashSet();
given(query.execute()).willReturn(coll);
JdoTemplate jt = new JdoTemplate(pmf);
assertEquals(coll, jt.find(String.class));
verify(pm).close();
}
@Test
public void testFindWithFilter() {
Query query = mock(Query.class);
given(pmf.getPersistenceManager()).willReturn(pm);
given(pm.newQuery(String.class, "a == b")).willReturn(query);
Collection coll = new HashSet();
given(query.execute()).willReturn(coll);
JdoTemplate jt = new JdoTemplate(pmf);
assertEquals(coll, jt.find(String.class, "a == b"));
verify(pm).close();
}
@Test
public void testFindWithFilterAndOrdering() {
Query query = mock(Query.class);
given(pmf.getPersistenceManager()).willReturn(pm);
given(pm.newQuery(String.class, "a == b")).willReturn(query);
Collection coll = new HashSet();
given(query.execute()).willReturn(coll);
JdoTemplate jt = new JdoTemplate(pmf);
assertEquals(coll, jt.find(String.class, "a == b", "c asc"));
verify(query).setOrdering("c asc");
verify(pm).close();
}
@Test
public void testFindWithParameterArray() {
Query query = mock(Query.class);
given(pmf.getPersistenceManager()).willReturn(pm);
given(pm.newQuery(String.class, "a == b")).willReturn(query);
Object[] values = new Object[0];
Collection coll = new HashSet();
given(query.executeWithArray(values)).willReturn(coll);
JdoTemplate jt = new JdoTemplate(pmf);
assertEquals(coll, jt.find(String.class, "a == b", "params", values));
verify(query).declareParameters("params");
verify(pm).close();
}
@Test
public void testFindWithParameterArrayAndOrdering() {
Query query = mock(Query.class);
given(pmf.getPersistenceManager()).willReturn(pm);
given(pm.newQuery(String.class, "a == b")).willReturn(query);
Object[] values = new Object[0];
Collection coll = new HashSet();
given(query.executeWithArray(values)).willReturn(coll);
JdoTemplate jt = new JdoTemplate(pmf);
assertEquals(coll, jt.find(String.class, "a == b", "params", values, "c asc"));
verify(query).declareParameters("params");
verify(query).setOrdering("c asc");
verify(pm).close();
}
@Test
public void testFindWithParameterMap() {
Query query = mock(Query.class);
given(pmf.getPersistenceManager()).willReturn(pm);
given(pm.newQuery(String.class, "a == b")).willReturn(query);
Map values = new HashMap();
Collection coll = new HashSet();
given(query.executeWithMap(values)).willReturn(coll);
JdoTemplate jt = new JdoTemplate(pmf);
assertEquals(coll, jt.find(String.class, "a == b", "params", values));
verify(query).declareParameters("params");
verify(pm).close();
}
@Test
public void testFindWithParameterMapAndOrdering() {
Query query = mock(Query.class);
given(pmf.getPersistenceManager()).willReturn(pm);
given(pm.newQuery(String.class, "a == b")).willReturn(query);
Map values = new HashMap();
Collection coll = new HashSet();
given(query.executeWithMap(values)).willReturn(coll);
JdoTemplate jt = new JdoTemplate(pmf);
assertEquals(coll, jt.find(String.class, "a == b", "params", values, "c asc"));
verify(query).declareParameters("params");
verify(query).setOrdering("c asc");
verify(pm).close();
}
@Test
public void testFindWithLanguageAndQueryObject() {
Query query = mock(Query.class);
given(pmf.getPersistenceManager()).willReturn(pm);
given(pm.newQuery(Query.SQL, "some SQL")).willReturn(query);
Collection coll = new HashSet();
given(query.execute()).willReturn(coll);
JdoTemplate jt = new JdoTemplate(pmf);
assertEquals(coll, jt.find(Query.SQL, "some SQL"));
verify(pm).close();
}
@Test
public void testFindWithQueryString() {
Query query = mock(Query.class);
given(pmf.getPersistenceManager()).willReturn(pm);
given(pm.newQuery("single string query")).willReturn(query);
Collection coll = new HashSet();
given(query.execute()).willReturn(coll);
JdoTemplate jt = new JdoTemplate(pmf);
assertEquals(coll, jt.find("single string query"));
verify(pm).close();
}
@Test
public void testFindByNamedQuery() {
Query query = mock(Query.class);
given(pmf.getPersistenceManager()).willReturn(pm);
given(pm.newNamedQuery(String.class, "some query name")).willReturn(query);
Collection coll = new HashSet();
given(query.execute()).willReturn(coll);
JdoTemplate jt = new JdoTemplate(pmf);
assertEquals(coll, jt.findByNamedQuery(String.class, "some query name"));
verify(pm).close();
}
@Test
public void testTemplateExceptions() {
try {
createTemplate().execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm) {
throw new JDOObjectNotFoundException();
}
});
fail("Should have thrown JdoObjectRetrievalFailureException");
}
catch (JdoObjectRetrievalFailureException ex) {
// expected
}
try {
createTemplate().execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm) {
throw new JDOOptimisticVerificationException();
}
});
fail("Should have thrown JdoOptimisticLockingFailureException");
}
catch (JdoOptimisticLockingFailureException ex) {
// expected
}
try {
createTemplate().execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm) {
throw new JDODataStoreException();
}
});
fail("Should have thrown JdoResourceFailureException");
}
catch (JdoResourceFailureException ex) {
// expected
}
try {
createTemplate().execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm) {
throw new JDOFatalDataStoreException();
}
});
fail("Should have thrown JdoResourceFailureException");
}
catch (JdoResourceFailureException ex) {
// expected
}
try {
createTemplate().execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm) {
throw new JDOUserException();
}
});
fail("Should have thrown JdoUsageException");
}
catch (JdoUsageException ex) {
// expected
}
try {
createTemplate().execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm) {
throw new JDOFatalUserException();
}
});
fail("Should have thrown JdoUsageException");
}
catch (JdoUsageException ex) {
// expected
}
try {
createTemplate().execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm) {
throw new JDOException();
}
});
fail("Should have thrown JdoSystemException");
}
catch (JdoSystemException ex) {
// expected
}
}
@Test
public void testTranslateException() {
JdoDialect dialect = mock(JdoDialect.class);
final JDOException ex = new JDOException();
given(dialect.translateException(ex)).willReturn(new DataIntegrityViolationException("test", ex));
try {
JdoTemplate template = createTemplate();
template.setJdoDialect(dialect);
template.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm) {
throw ex;
}
});
fail("Should have thrown DataIntegrityViolationException");
}
catch (DataIntegrityViolationException dive) {
// expected
}
}
private JdoTemplate createTemplate() {
given(pmf.getPersistenceManager()).willReturn(pm);
return new JdoTemplate(pmf);
}
}

View File

@ -16,19 +16,16 @@
package org.springframework.orm.jdo;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.util.ArrayList;
import java.util.List;
import javax.jdo.Constants;
import javax.jdo.JDOFatalDataStoreException;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;
import javax.jdo.Transaction;
import javax.sql.DataSource;
import javax.transaction.Status;
@ -38,12 +35,12 @@ import javax.transaction.UserTransaction;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.jdbc.datasource.ConnectionHandle;
import org.springframework.jdbc.datasource.ConnectionHolder;
import org.springframework.jdbc.datasource.SimpleConnectionHandle;
import org.springframework.orm.jdo.support.SpringPersistenceManagerProxyBean;
import org.springframework.orm.jdo.support.StandardPersistenceManagerProxyBean;
import org.springframework.tests.sample.beans.TestBean;
import org.springframework.tests.transaction.MockJtaTransaction;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
@ -124,14 +121,8 @@ public class JdoTransactionManagerTests {
PersistenceManager stdPmProxy = stdProxyBean.getObject();
stdPmProxy.flush();
JdoTemplate jt = new JdoTemplate(pmf);
return jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm2) {
pm2.flush();
return l;
}
});
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true).flush();
return l;
}
});
assertTrue("Correct result list", result == l);
@ -161,13 +152,8 @@ public class JdoTransactionManagerTests {
@Override
public Object doInTransaction(TransactionStatus status) {
assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
JdoTemplate jt = new JdoTemplate(pmf);
return jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm) {
throw new RuntimeException("application exception");
}
});
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true);
throw new RuntimeException("application exception");
}
});
fail("Should have thrown RuntimeException");
@ -199,13 +185,8 @@ public class JdoTransactionManagerTests {
@Override
public Object doInTransaction(TransactionStatus status) {
assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
JdoTemplate jt = new JdoTemplate(pmf);
return jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm) {
throw new RuntimeException("application exception");
}
});
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true);
throw new RuntimeException("application exception");
}
});
fail("Should have thrown RuntimeException");
@ -235,14 +216,7 @@ public class JdoTransactionManagerTests {
@Override
public Object doInTransaction(TransactionStatus status) {
assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
JdoTemplate jt = new JdoTemplate(pmf);
jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm2) {
pm2.flush();
return null;
}
});
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true).flush();
status.setRollbackOnly();
return null;
}
@ -274,14 +248,8 @@ public class JdoTransactionManagerTests {
return tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
JdoTemplate jt = new JdoTemplate(pmf);
return jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm2) {
pm2.flush();
return l;
}
});
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true).flush();
return l;
}
});
}
@ -308,13 +276,8 @@ public class JdoTransactionManagerTests {
return tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
JdoTemplate jt = new JdoTemplate(pmf);
return jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm) {
throw new RuntimeException("application exception");
}
});
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true);
throw new RuntimeException("application exception");
}
});
}
@ -350,14 +313,7 @@ public class JdoTransactionManagerTests {
return tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
JdoTemplate jt = new JdoTemplate(pmf);
jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm2) {
pm2.flush();
return l;
}
});
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true).flush();
status.setRollbackOnly();
return null;
}
@ -393,14 +349,8 @@ public class JdoTransactionManagerTests {
return tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
JdoTemplate jt = new JdoTemplate(pmf);
return jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm2) {
pm2.flush();
return l;
}
});
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true).flush();
return l;
}
});
}
@ -430,25 +380,13 @@ public class JdoTransactionManagerTests {
Object result = tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
JdoTemplate jt = new JdoTemplate(pmf);
jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm2) {
return null;
}
});
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true);
return tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
JdoTemplate jt = new JdoTemplate(pmf);
return jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm2) {
pm2.flush();
return l;
}
});
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true).flush();
return l;
}
});
}
@ -483,25 +421,10 @@ public class JdoTransactionManagerTests {
public Object doInTransaction(TransactionStatus status) {
assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive());
assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
JdoTemplate jt = new JdoTemplate(pmf);
jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm2) {
assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
pm2.flush();
return l;
}
});
Object result = jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm2) {
assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
pm2.flush();
return l;
}
});
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true).flush();
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true).flush();
assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
return result;
return l;
}
});
assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
@ -545,25 +468,13 @@ public class JdoTransactionManagerTests {
catch (Exception ex) {
}
JdoTemplate jt = new JdoTemplate(pmf);
jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm2) {
return null;
}
});
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true);
return tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
JdoTemplate jt = new JdoTemplate(pmf);
return jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm2) {
pm2.flush();
return l;
}
});
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true).flush();
return l;
}
});
}
@ -595,13 +506,8 @@ public class JdoTransactionManagerTests {
public Object doInTransaction(TransactionStatus status) {
assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
assertTrue("Is not new transaction", !status.isNewTransaction());
JdoTemplate jt = new JdoTemplate(pmf);
return jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm) {
return l;
}
});
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true);
return l;
}
});
assertTrue("Correct result list", result == l);
@ -644,13 +550,8 @@ public class JdoTransactionManagerTests {
@Override
public Object doInTransaction(TransactionStatus status) {
assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
JdoTemplate jt = new JdoTemplate(pmf);
return jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm) {
return l;
}
});
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true);
return l;
}
});
assertTrue("Correct result list", result == l);
@ -689,13 +590,8 @@ public class JdoTransactionManagerTests {
public Object doInTransaction(TransactionStatus status) {
assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
assertTrue("Has thread con", TransactionSynchronizationManager.hasResource(ds));
JdoTemplate jt = new JdoTemplate(pmf);
return jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm) {
return l;
}
});
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true);
return l;
}
});
assertTrue("Correct result list", result == l);
@ -737,13 +633,8 @@ public class JdoTransactionManagerTests {
public Object doInTransaction(TransactionStatus status) {
assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
assertTrue("Has thread con", TransactionSynchronizationManager.hasResource(ds));
JdoTemplate jt = new JdoTemplate(pmf);
return jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm) {
return l;
}
});
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true);
return l;
}
});
assertTrue("Correct result list", result == l);
@ -783,16 +674,8 @@ public class JdoTransactionManagerTests {
public Object doInTransaction(TransactionStatus status) {
assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
assertTrue("Hasn't thread con", !TransactionSynchronizationManager.hasResource(ds));
JdoTemplate jt = new JdoTemplate();
jt.setPersistenceManagerFactory(pmf);
jt.setJdoDialect(dialect);
return jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm2) {
pm2.flush();
return l;
}
});
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true).flush();
return l;
}
});
assertTrue("Correct result list", result == l);
@ -866,14 +749,8 @@ public class JdoTransactionManagerTests {
}
});
}
JdoTemplate jt = new JdoTemplate(pmf);
return jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm2) {
pm2.flush();
return l;
}
});
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true).flush();
return l;
}
});
assertTrue("Correct result list", result == l);
@ -890,72 +767,6 @@ public class JdoTransactionManagerTests {
verify(tx).commit();
}
@Test
public void testTransactionTimeoutWithJdoDialect() throws SQLException {
doTestTransactionTimeoutWithJdoDialect(true);
}
@Test
public void testTransactionTimeoutWithJdoDialectAndPmProxy() throws SQLException {
doTestTransactionTimeoutWithJdoDialect(false);
}
private void doTestTransactionTimeoutWithJdoDialect(final boolean exposeNativePm) throws SQLException {
Query query = mock(Query.class);
final JdoDialect dialect = mock(JdoDialect.class);
TransactionTemplate tt = new TransactionTemplate();
given(pmf.getPersistenceManager()).willReturn(pm);
given(pm.currentTransaction()).willReturn(tx);
if (!exposeNativePm) {
dialect.applyQueryTimeout(query, 10);
}
given(pm.newQuery(TestBean.class)).willReturn(query);
JdoTransactionManager tm = new JdoTransactionManager(pmf);
tm.setJdoDialect(dialect);
tt.setTransactionManager(tm);
tt.setTimeout(10);
assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
JdoTemplate jt = new JdoTemplate(pmf);
jt.setJdoDialect(dialect);
if (exposeNativePm) {
jt.setExposeNativePersistenceManager(true);
}
return jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm2) {
if (exposeNativePm) {
assertSame(pm, pm2);
}
else {
assertTrue(Proxy.isProxyClass(pm2.getClass()));
}
pm2.newQuery(TestBean.class);
return null;
}
});
}
});
assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
verify(dialect).beginTransaction(tx, tt);
verify(dialect).cleanupTransaction(null);
verify(pm).close();
verify(tx).getRollbackOnly();
verify(tx).commit();
}
@Test
public void testTransactionFlush() {
given(pmf.getPersistenceManager()).willReturn(pm);

View File

@ -1,71 +0,0 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.orm.jdo.support;
import java.util.ArrayList;
import java.util.List;
import javax.jdo.PersistenceManagerFactory;
import org.junit.Test;
import org.springframework.orm.jdo.JdoTemplate;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
/**
* @author Juergen Hoeller
* @author Phillip Webb
* @since 30.07.2003
*/
public class JdoDaoSupportTests {
@Test
public void testJdoDaoSupportWithPersistenceManagerFactory() throws Exception {
PersistenceManagerFactory pmf = mock(PersistenceManagerFactory.class);
pmf.getConnectionFactory();
final List test = new ArrayList();
JdoDaoSupport dao = new JdoDaoSupport() {
@Override
protected void initDao() {
test.add("test");
}
};
dao.setPersistenceManagerFactory(pmf);
dao.afterPropertiesSet();
assertEquals("Correct PersistenceManagerFactory", pmf, dao.getPersistenceManagerFactory());
assertEquals("Correct JdoTemplate", pmf, dao.getJdoTemplate().getPersistenceManagerFactory());
assertEquals("initDao called", test.size(), 1);
}
@Test
public void testJdoDaoSupportWithJdoTemplate() throws Exception {
JdoTemplate template = new JdoTemplate();
final List test = new ArrayList();
JdoDaoSupport dao = new JdoDaoSupport() {
@Override
protected void initDao() {
test.add("test");
}
};
dao.setJdoTemplate(template);
dao.afterPropertiesSet();
assertEquals("Correct JdoTemplate", template, dao.getJdoTemplate());
assertEquals("initDao called", test.size(), 1);
}
}