spring的例子jpetstore的dao使用了SqlMapClientFactoryBean实现了,DAO的单例
如果我别的部分不动
只是将数据库访问换成JDBC
如何实现DAO的单例呢
好像SPRING没提供JDBC的DAO的单例啊,我现在软件跟踪显示DAO被生成了多个,求解..
dataAccessContext-local.xml:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}">
<property name="jdbcUrl" value="${jdbc.url}">
<property name="user" value="${jdbc.username}">
<property name="password" value="${jdbc.password}">
<property name="acquireIncrement" value="1">
<property name="maxPoolSize" value="500">
<property name="minPoolSize" value="10">
<property name="initialPoolSize" value="10">
<property name="maxIdleTime" value="20">
<property name="acquireRetryAttempts" value="3">
</bean>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource">
</bean>
<bean id="customerDao" class="cn.co.unicom.crm.dao.CustomerDao">
<property name="dataSource" ref="dataSource">
</bean>
applicationContext.xml:
<aop:config>
<aop:advisor pointcut="execution(* ..ilogic..*(..))" <br=""> advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="create*" <br=""> rollback-for="cn.co.unicom.framework.exception.CrmException" />
<tx:method name="store*" <br=""> rollback-for="cn.co.unicom.framework.exception.CrmException" />
<tx:method name="drop*" <br=""> rollback-for="cn.co.unicom.framework.exception.CrmException" />
<tx:method name="find*" read-only="true">
</tx:attributes>
</tx:advice>
<bean id="dbSearchBusiness" class="cn.co.unicom.crm.logic.DBSearchBusiness">
<property name="customerDao" ref="customerDao">
</bean>
问题补充:
我的代码只是换了jdbc
但是测试软件显示,没实现真正意义的单根
出现了如图的现象
这样对象很多,给服务造成了很大的压力
问题补充:
public class BaseDao extends JdbcDaoSupport {
}
public interface IBusinessDao {
abstract List<business> selectBusiness() throws CrmException;
}
public class BusinessDao extends BaseDao implements IBusinessDao {
public static final String SELECT_BUSINESS_SQL =
"SELECT b.business_id," +
" b.business_name " +
" FROM md_business b";
/* (non-Javadoc)
* @see cn.co.unicom.crm.dao.IBusinessDao#selectBusiness()
*/
public List<business> selectBusiness() throws CrmException {
List<business> businessList = new ArrayList<business>();
RowMapper rowMapper = new RowMapper() {
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
Business business = new Business();
business.setBusinessId(rs.getString("business_id"));
business.setBusinessName(rs.getString("business_name"));
return business;
}
};
try {
businessList = getJdbcTemplate().query(SELECT_BUSINESS_SQL,
rowMapper);
} catch (DataAccessException e) {
throw new CrmException(e.getMessage(), e, "E");
}
if (businessList.size() == 0) {
throw new CrmException("ビジネス区分を取得しません。", "W");
}
return businessList;
}
问题补充:
我个人判断原因是SqlMapClientFactoryBean,起了效果
但是JDBC是没有这个factory的
造成的
求解
两点:
1.单例与否与性能无关,新的JVM对年轻态的对象回收非常高效。
2.图中你画出来的,一个是实例,另一个是经过动态代理的实例,经过代理的那个可能会有很多实例。
结论:你的用法没错,程序也OK,不必担心。
[quote]
[/quote]
这已经说明customerDao是单件的. Spring里面其他的bean对这个customerDao的bean的使用都是单件引用. 比如: 你的dbSearchBusiness里面注入的customerDao.
除非你将bean的 singleton="false",那么这个bean才不是单件的.
难道你说的是不是用iBATIS, 而直接使用JDBC?
那么你可以使用Spring提供的JdbcTemplate.
让你的dao去继承JdbcTemplate即可, 而不是继承iBATIS的SqlMapClientTemplate.