1 16 17 package org.springframework.orm.jdo; 18 19 import java.sql.Connection ; 20 import java.sql.SQLException ; 21 import java.util.Collection ; 22 23 import javax.jdo.JDOException; 24 import javax.jdo.PersistenceManager; 25 import javax.jdo.Query; 26 import javax.jdo.Transaction; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 31 import org.springframework.dao.DataAccessException; 32 import org.springframework.dao.support.PersistenceExceptionTranslator; 33 import org.springframework.jdbc.datasource.ConnectionHandle; 34 import org.springframework.jdbc.support.JdbcUtils; 35 import org.springframework.jdbc.support.SQLExceptionTranslator; 36 import org.springframework.transaction.InvalidIsolationLevelException; 37 import org.springframework.transaction.TransactionDefinition; 38 import org.springframework.transaction.TransactionException; 39 40 70 public class DefaultJdoDialect implements JdoDialect, PersistenceExceptionTranslator { 71 72 protected final Log logger = LogFactory.getLog(getClass()); 73 74 private SQLExceptionTranslator jdbcExceptionTranslator; 75 76 77 80 public DefaultJdoDialect() { 81 } 82 83 90 DefaultJdoDialect(Object connectionFactory) { 91 this.jdbcExceptionTranslator = PersistenceManagerFactoryUtils.newJdbcExceptionTranslator(connectionFactory); 92 } 93 94 104 public void setJdbcExceptionTranslator(SQLExceptionTranslator jdbcExceptionTranslator) { 105 this.jdbcExceptionTranslator = jdbcExceptionTranslator; 106 } 107 108 111 public SQLExceptionTranslator getJdbcExceptionTranslator() { 112 return this.jdbcExceptionTranslator; 113 } 114 115 116 120 127 public Object beginTransaction(Transaction transaction, TransactionDefinition definition) 128 throws JDOException, SQLException , TransactionException { 129 130 if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) { 131 throw new InvalidIsolationLevelException( 132 "Standard JDO does not support custom isolation levels: " + 133 "use a special JdoDialect implementation for your JDO provider"); 134 } 135 transaction.begin(); 136 return null; 137 } 138 139 144 public void cleanupTransaction(Object transactionData) { 145 } 146 147 168 public ConnectionHandle getJdbcConnection(PersistenceManager pm, boolean readOnly) 169 throws JDOException, SQLException { 170 171 return new DataStoreConnectionHandle(pm); 172 } 173 174 182 public void releaseJdbcConnection(ConnectionHandle conHandle, PersistenceManager pm) 183 throws JDOException, SQLException { 184 } 185 186 187 191 197 public Object detachCopy(PersistenceManager pm, Object entity) throws JDOException { 198 return pm.detachCopy(entity); 199 } 200 201 207 public Collection detachCopyAll(PersistenceManager pm, Collection entities) throws JDOException { 208 return pm.detachCopyAll(entities); 209 } 210 211 222 public Object attachCopy(PersistenceManager pm, Object detachedEntity) throws JDOException { 223 return pm.makePersistent(detachedEntity); 224 } 225 226 237 public Collection attachCopyAll(PersistenceManager pm, Collection detachedEntities) throws JDOException { 238 return pm.makePersistentAll(detachedEntities); 239 } 240 241 247 public void flush(PersistenceManager pm) throws JDOException { 248 pm.flush(); 249 } 250 251 257 public Query newNamedQuery(PersistenceManager pm, Class entityClass, String queryName) throws JDOException { 258 return pm.newNamedQuery(entityClass, queryName); 259 } 260 261 264 public void applyQueryTimeout(Query query, int remainingTimeInSeconds) throws JDOException { 265 logger.info("DefaultJdoDialect does not support query timeouts: ignoring remaining transaction time"); 266 } 267 268 269 273 281 public DataAccessException translateExceptionIfPossible(RuntimeException ex) { 282 if (ex instanceof JDOException) { 283 return translateException((JDOException) ex); 284 } 285 return null; 286 } 287 288 292 public DataAccessException translateException(JDOException ex) { 293 if (getJdbcExceptionTranslator() != null && ex.getCause() instanceof SQLException ) { 294 return getJdbcExceptionTranslator().translate("JDO operation: " + ex.getMessage(), 295 extractSqlStringFromException(ex), (SQLException ) ex.getCause()); 296 } 297 return PersistenceManagerFactoryUtils.convertJdoAccessException(ex); 298 } 299 300 307 protected String extractSqlStringFromException(JDOException ex) { 308 return null; 309 } 310 311 312 319 private static class DataStoreConnectionHandle implements ConnectionHandle { 320 321 private final PersistenceManager persistenceManager; 322 323 public DataStoreConnectionHandle(PersistenceManager persistenceManager) { 324 this.persistenceManager = persistenceManager; 325 } 326 327 public Connection getConnection() { 328 return (Connection ) this.persistenceManager.getDataStoreConnection(); 329 } 330 331 public void releaseConnection(Connection con) { 332 JdbcUtils.closeConnection(con); 333 } 334 } 335 336 } 337 | Popular Tags |