1 16 17 package org.springframework.jdbc.support; 18 19 import javax.sql.DataSource ; 20 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 24 import org.springframework.beans.factory.InitializingBean; 25 26 38 public abstract class JdbcAccessor implements InitializingBean { 39 40 41 protected final Log logger = LogFactory.getLog(getClass()); 42 43 private DataSource dataSource; 44 45 private SQLExceptionTranslator exceptionTranslator; 46 47 private boolean lazyInit = true; 48 49 50 53 public void setDataSource(DataSource dataSource) { 54 this.dataSource = dataSource; 55 } 56 57 60 public DataSource getDataSource() { 61 return this.dataSource; 62 } 63 64 72 public void setDatabaseProductName(String dbName) { 73 this.exceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(dbName); 74 } 75 76 84 public void setExceptionTranslator(SQLExceptionTranslator exceptionTranslator) { 85 this.exceptionTranslator = exceptionTranslator; 86 } 87 88 95 public synchronized SQLExceptionTranslator getExceptionTranslator() { 96 if (this.exceptionTranslator == null) { 97 DataSource dataSource = getDataSource(); 98 if (dataSource != null) { 99 this.exceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(dataSource); 100 } 101 else { 102 this.exceptionTranslator = new SQLStateSQLExceptionTranslator(); 103 } 104 } 105 return this.exceptionTranslator; 106 } 107 108 116 public void setLazyInit(boolean lazyInit) { 117 this.lazyInit = lazyInit; 118 } 119 120 124 public boolean isLazyInit() { 125 return this.lazyInit; 126 } 127 128 132 public void afterPropertiesSet() { 133 if (getDataSource() == null) { 134 throw new IllegalArgumentException ("Property 'dataSource' is required"); 135 } 136 if (!isLazyInit()) { 137 getExceptionTranslator(); 138 } 139 } 140 141 } 142 | Popular Tags |