1 16 17 package org.springframework.orm.toplink; 18 19 import java.sql.SQLException ; 20 21 import oracle.toplink.exceptions.DatabaseException; 22 import oracle.toplink.exceptions.TopLinkException; 23 24 import org.springframework.beans.factory.BeanClassLoaderAware; 25 import org.springframework.beans.factory.DisposableBean; 26 import org.springframework.beans.factory.FactoryBean; 27 import org.springframework.beans.factory.InitializingBean; 28 import org.springframework.dao.DataAccessException; 29 import org.springframework.dao.support.PersistenceExceptionTranslator; 30 import org.springframework.jdbc.support.SQLExceptionTranslator; 31 32 63 public class LocalSessionFactoryBean extends LocalSessionFactory 64 implements FactoryBean, BeanClassLoaderAware, InitializingBean, DisposableBean, PersistenceExceptionTranslator { 65 66 private SessionFactory sessionFactory; 67 68 private SQLExceptionTranslator jdbcExceptionTranslator; 69 70 71 80 public void setJdbcExceptionTranslator(SQLExceptionTranslator jdbcExceptionTranslator) { 81 this.jdbcExceptionTranslator = jdbcExceptionTranslator; 82 } 83 84 87 public SQLExceptionTranslator getJdbcExceptionTranslator() { 88 return this.jdbcExceptionTranslator; 89 } 90 91 95 public void setBeanClassLoader(ClassLoader classLoader) { 96 setSessionClassLoader(classLoader); 97 } 98 99 public void afterPropertiesSet() throws TopLinkException { 100 this.sessionFactory = createSessionFactory(); 101 } 102 103 104 public Object getObject() { 105 return this.sessionFactory; 106 } 107 108 public Class getObjectType() { 109 return (this.sessionFactory != null ? this.sessionFactory.getClass() : SessionFactory.class); 110 } 111 112 public boolean isSingleton() { 113 return true; 114 } 115 116 117 125 public DataAccessException translateExceptionIfPossible(RuntimeException ex) { 126 if (ex instanceof TopLinkException) { 127 return convertTopLinkAccessException((TopLinkException) ex); 128 } 129 return null; 130 } 131 132 142 public DataAccessException convertTopLinkAccessException(TopLinkException ex) { 143 if (getJdbcExceptionTranslator() != null && ex instanceof DatabaseException) { 144 Throwable internalEx = ex.getInternalException(); 145 if (internalEx instanceof SQLException ) { 147 return getJdbcExceptionTranslator().translate( 148 "TopLink operation: " + ex.getMessage(), null, (SQLException ) internalEx); 149 } 150 } 151 return SessionFactoryUtils.convertTopLinkAccessException(ex); 152 } 153 154 155 public void destroy() { 156 logger.info("Closing TopLink SessionFactory"); 157 this.sessionFactory.close(); 158 } 159 160 } 161 | Popular Tags |