JdoDialect.java
上传用户:jiancairen
上传日期:2007-08-27
资源大小:26458k
文件大小:9k
源码类别:

Java编程

开发平台:

Java

  1. /*
  2.  * Copyright 2002-2004 the original author or authors.
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */ 
  16. package org.springframework.orm.jdo;
  17. import java.sql.SQLException;
  18. import javax.jdo.JDOException;
  19. import javax.jdo.PersistenceManager;
  20. import javax.jdo.Query;
  21. import javax.jdo.Transaction;
  22. import org.springframework.dao.DataAccessException;
  23. import org.springframework.jdbc.datasource.ConnectionHandle;
  24. import org.springframework.transaction.TransactionDefinition;
  25. import org.springframework.transaction.TransactionException;
  26. /**
  27.  * SPI strategy that encapsulates certain functionality that standard JDO 1.0 does
  28.  * not offer despite being relevant in the context of O/R mapping, like access to
  29.  * the underlying JDBC Connection and explicit flushing of changes to the database.
  30.  *
  31.  * <p>To be implemented for specific JDO implementations like Kodo, Lido, or JPOX.
  32.  * Almost every O/R-based JDO implementation offers proprietary means to access the
  33.  * underlying JDBC Connection and to explicitly flush changes. JDO 2.0 respectively
  34.  * JDO/R 2.0 are likely to define standard ways for these: If applicable, a JdoDialect
  35.  * implementation for JDO 2.0 will be provided to leverage them with Spring's JDO support.
  36.  *
  37.  * @author Juergen Hoeller
  38.  * @since 02.11.2003
  39.  * @see JdoTransactionManager#setJdoDialect
  40.  * @see JdoAccessor#setJdoDialect
  41.  */
  42. public interface JdoDialect {
  43. /**
  44.  * Begin the given JDO transaction, applying the semantics specified by the
  45.  * given Spring transaction definition (in particular, an isolation level
  46.  * and a timeout). Invoked by JdoTransactionManager on transaction begin.
  47.  * <p>An implementation can configure the JDO Transaction object and then
  48.  * invoke <code>begin</code>, or invoke a special begin method that takes,
  49.  * for example, an isolation level.
  50.  * <p>An implementation can also apply read-only flag and isolation level to the
  51.  * underlying JDBC Connection before beginning the transaction. In that case,
  52.  * a transaction data object can be returned that holds the previous isolation
  53.  * level (and possibly other data), to be reset in cleanupTransaction.
  54.  * @param transaction the JDO transaction to begin
  55.  * @param definition the Spring transaction definition that defines semantics
  56.  * @return an arbitrary object that holds transaction data, if any
  57.  * (to be passed into cleanupTransaction)
  58.  * @throws JDOException if thrown by JDO methods
  59.  * @throws SQLException if thrown by JDBC methods
  60.  * @throws TransactionException in case of invalid arguments
  61.  * @see #cleanupTransaction
  62.  * @see javax.jdo.Transaction#begin
  63.  * @see org.springframework.jdbc.datasource.DataSourceUtils#prepareConnectionForTransaction
  64.  */
  65. Object beginTransaction(Transaction transaction, TransactionDefinition definition)
  66. throws JDOException, SQLException, TransactionException;
  67. /**
  68.  * Clean up the transaction via the given transaction data.
  69.  * Invoked by JdoTransactionManager on transaction cleanup.
  70.  * <p>An implementation can, for example, reset read-only flag and
  71.  * isolation level of the underlying JDBC Connection.
  72.  * @param transactionData arbitrary object that holds transaction data, if any
  73.  * (as returned by beginTransaction)
  74.  * @see #beginTransaction
  75.  * @see org.springframework.jdbc.datasource.DataSourceUtils#resetConnectionAfterTransaction
  76.  */
  77. void cleanupTransaction(Object transactionData);
  78. /**
  79.  * Retrieve the JDBC Connection that the given JDO PersistenceManager uses underneath,
  80.  * if accessing a relational database. This method will just get invoked if actually
  81.  * needing access to the underlying JDBC Connection, usually within an active JDO
  82.  * transaction (for example, by JdoTransactionManager). The returned handle will
  83.  * be passed into the <code>releaseJdbcConnection</code> method when not needed anymore.
  84.  * <p>This strategy is necessary as JDO 1.0 does not provide a standard way to retrieve
  85.  * the underlying JDBC Connection (due to the fact that a JDO implementation might not
  86.  * work with a relational database at all).
  87.  * <p>Implementations are encouraged to return an unwrapped Connection object, i.e.
  88.  * the Connection as they got it from the connection pool. This makes it easier for
  89.  * application code to get at the underlying native JDBC Connection, like an
  90.  * OracleConnection, which is sometimes necessary for LOB handling etc. We assume
  91.  * that calling code knows how to properly handle the returned Connection object.
  92.  * <p>In a simple case where the returned Connection will be auto-closed with the
  93.  * PersistenceManager or can be released via the Connection object itself, an
  94.  * implementation can return a SimpleConnectionHandle that just contains the
  95.  * Connection. If some other object is needed in <code>releaseJdbcConnection</code>,
  96.  * an implementation should use a special handle that references that other object.
  97.  * @param pm the current JDO PersistenceManager
  98.  * @return a handle for the JDBC Connection, to be passed into
  99.  * <code>releaseJdbcConnection</code>, or null if no JDBC Connection can be retrieved
  100.  * @throws JDOException if thrown by JDO methods
  101.  * @throws SQLException if thrown by JDBC methods
  102.  * @see #releaseJdbcConnection
  103.  * @see org.springframework.jdbc.datasource.ConnectionHandle#getConnection
  104.  * @see org.springframework.jdbc.datasource.SimpleConnectionHandle
  105.  * @see JdoTransactionManager#setDataSource
  106.  * @see org.springframework.jdbc.support.nativejdbc.NativeJdbcExtractor
  107.  */
  108. ConnectionHandle getJdbcConnection(PersistenceManager pm, boolean readOnly)
  109. throws JDOException, SQLException;
  110. /**
  111.  * Release the given JDBC Connection, which has originally been retrieved
  112.  * via <code>getJdbcConnection</code>. This should be invoked in any case,
  113.  * to allow for proper release of the retrieved Connection handle.
  114.  * <p>An implementation might simply do nothing, if the Connection returned
  115.  * by <code>getJdbcConnection</code> will be implicitly closed when the JDO
  116.  * transaction completes respectively when the PersistenceManager is closed.
  117.  * @param conHandle the JDBC Connection handle to release
  118.  * @param pm the current JDO PersistenceManager
  119.  * @throws JDOException if thrown by JDO methods
  120.  * @throws SQLException if thrown by JDBC methods
  121.  * @see #getJdbcConnection
  122.  */
  123. void releaseJdbcConnection(ConnectionHandle conHandle, PersistenceManager pm)
  124. throws JDOException, SQLException;
  125. /**
  126.  * Apply the given timeout to the given JDO query object.
  127.  * <p>Invoked by JdoTemplate with the remaining time of a specified
  128.  * transaction timeout, if any.
  129.  * @param query the JDO query object to apply the timeout to
  130.  * @param timeout the timeout value to apply
  131.  * @throws JDOException if thrown by JDO methods
  132.  * @see JdoTemplate#prepareQuery
  133.  */
  134. void applyQueryTimeout(Query query, int timeout) throws JDOException;
  135. /**
  136.  * Flush the given PersistenceManager, i.e. flush all changes (that have been
  137.  * applied to persistent objects) to the underlying database. This method will
  138.  * just get invoked when eager flushing is actually necessary, for example when
  139.  * JDBC access code needs to see changes within the same transaction.
  140.  * @param pm the current JDO PersistenceManager
  141.  * @throws JDOException in case of errors
  142.  * @see JdoAccessor#setFlushEager
  143.  */
  144. void flush(PersistenceManager pm) throws JDOException;
  145. /**
  146.  * Translate the given JDOException to a corresponding exception from Spring's
  147.  * generic DataAccessException hierarchy. An implementation should apply
  148.  * PersistenceManagerFactoryUtils' standard exception translation if can't do
  149.  * anything more specific.
  150.  * <p>Of particular importance is the correct translation to
  151.  * DataIntegrityViolationException, for example on constraint violation.
  152.  * Unfortunately, standard JDO does not allow for portable detection of this.
  153.  * <p>Can use a SQLExceptionTranslator for translating underlying SQLExceptions
  154.  * in a database-specific fashion.
  155.  * @param ex the JDOException thrown
  156.  * @return the corresponding DataAccessException (must not be null)
  157.  * @see JdoAccessor#convertJdoAccessException
  158.  * @see JdoTransactionManager#convertJdoAccessException
  159.  * @see PersistenceManagerFactoryUtils#convertJdoAccessException
  160.  * @see org.springframework.dao.DataIntegrityViolationException
  161.  * @see org.springframework.jdbc.support.SQLExceptionTranslator
  162.  */
  163. DataAccessException translateException(JDOException ex);
  164. }