1 16 17 package org.springframework.jdbc.core.support; 18 19 import java.sql.Connection ; 20 21 import javax.sql.DataSource ; 22 23 import org.springframework.dao.support.DaoSupport; 24 import org.springframework.jdbc.CannotGetJdbcConnectionException; 25 import org.springframework.jdbc.core.JdbcTemplate; 26 import org.springframework.jdbc.datasource.DataSourceUtils; 27 import org.springframework.jdbc.support.SQLExceptionTranslator; 28 29 46 public abstract class JdbcDaoSupport extends DaoSupport { 47 48 private JdbcTemplate jdbcTemplate; 49 50 51 54 public final void setDataSource(DataSource dataSource) { 55 this.jdbcTemplate = createJdbcTemplate(dataSource); 56 initTemplateConfig(); 57 } 58 59 68 protected JdbcTemplate createJdbcTemplate(DataSource dataSource) { 69 return new JdbcTemplate(dataSource); 70 } 71 72 75 public final DataSource getDataSource() { 76 return (this.jdbcTemplate != null ? this.jdbcTemplate.getDataSource() : null); 77 } 78 79 83 public final void setJdbcTemplate(JdbcTemplate jdbcTemplate) { 84 this.jdbcTemplate = jdbcTemplate; 85 initTemplateConfig(); 86 } 87 88 92 public final JdbcTemplate getJdbcTemplate() { 93 return this.jdbcTemplate; 94 } 95 96 104 protected void initTemplateConfig() { 105 } 106 107 protected void checkDaoConfig() { 108 if (this.jdbcTemplate == null) { 109 throw new IllegalArgumentException ("'dataSource' or 'jdbcTemplate' is required"); 110 } 111 } 112 113 114 119 protected final SQLExceptionTranslator getExceptionTranslator() { 120 return getJdbcTemplate().getExceptionTranslator(); 121 } 122 123 129 protected final Connection getConnection() throws CannotGetJdbcConnectionException { 130 return DataSourceUtils.getConnection(getDataSource()); 131 } 132 133 139 protected final void releaseConnection(Connection con) { 140 DataSourceUtils.releaseConnection(con, getDataSource()); 141 } 142 143 } 144 | Popular Tags |