使用Spring+Ibatis的项目,关于如何使用事务、如何提升性能的几点总结
来源:互联网 发布:数据分类汇总怎么做 编辑:程序博客网 时间:2024/05/20 02:25
1. 按需取数据,减少网络开销。
A 表:有100多个字段,存储在数据库A上
Id
Property1
Property2
…
Property120
B 表:只有10个字段,存储在数据库B上
Id
Property1
Property2
…
Property10
要从A表中查询一些数据在插入到B表中。
懒的做法:把所有数据全读出来,当获取2000条数据时,网络耗时约15秒。在项目过程中,有开发人员偷懒,就写了如下的sql,导致调用远程服务取数据时,设定服务超时时间为3秒,结果总是超时。
1. select * from A where …
2. 把从A表获得的数据转换成符合B表格式的数据
3. Insert into B(id,property1,…,property10) valuses(1,’test’,…,’2009-11-9’)
好的做法:只取B表需要的数据,耗时从15秒减少至2秒。
select id,property1,…,property10 from A where …
2. 在DAO层,Spring+Ibatis批量插入数据, 减少获取数据库连接池的次数,提高性能.
SqlMapClientTemplate sqlMapClientTemplate = getSqlDaoBaseSupport()
.getSqlMapTemplate(SubDBRoute.getCRMRoute());
sqlMapClientTemplate.execute(new SqlMapClientCallback() {
public Object doInSqlMapClient(SqlMapExecutor executor)
throws SQLException {
executor.startBatch();
for (int i = 0; i < resultList.size(); i++) {
executor.insert("IbatisTestDAO.insertTest",
resultList.get(i));
}
executor.executeBatch();
return null;
}
});
3. 使用Spring的事务,保证数据的一致性.
public boolean atomicInsert () throws RegRuleServiceException {
Object object = crmTransactionTemplate.execute(new TransactionCallback() {
public Object doInTransaction(TransactionStatus status) {
Boolean ret = Boolean.FALSE;
try {
doSomething();
ret = Boolean.TRUE;
} catch (DAOException e) {
logger.error("atomicInsert", e);
status.setRollbackOnly();
}
return ret;
}
});
return (Boolean) object;
}
4. 插入数据时Spring template的update和insert的区别。在项目过程中,开发人员在写代码时,忽视了这两个方法的区别,导致需要自动生成主键的插入不成功,花费了很长时间才找出原因。下次引以为戒。
<insert id="IbatisTestDAO.insertTest" parameterClass="TestDO">
<selectKey resultClass="long" keyProperty="id">
select SEQ_TEST_ID.nextval as id from dual
</selectKey>
insert into test(id,name) values(#id#,#name#)
</insert>
调用Spring template的update方法,也可以成功插入一条数据,返回的是被更新的记录数。这个时候selectKey的自动生成主键不起作用。
//调用Spring template
Int updateRows = sqlMapClientTemplate.update(statementName, parameterObject);
调用Spring template的insert方法,成功插入一条数据,并且返回新增加记录的主键。
//调用Spring template
Object back = sqlMapClientTemplate.insert(statementName, parameterObject);
- 使用Spring+Ibatis的项目,关于如何使用事务、如何提升性能的几点总结
- 使用Spring+Ibatis的项目,关于如何使用事务、如何提升性能的几点总结
- 如何处理Spring、Ibatis结合MySQL数据库使用时的事务操作
- 关于使用mybatis的几点总结
- 使用事务提升sqlite insert的性能
- 关于磁盘性能的几点总结
- 关于初级程序员职场如何提升技能的几点建议?
- 关于初级程序员职场如何提升技能的几点建议?
- java编程循环语句使用中提升性能的几点建议
- 关于使用存储过程的好处的几点总结
- 关于如何使用动态域名在个人电脑搭建个人网站的几点经验
- 关于使用python logging模块的几点总结
- 关于Jenkins使用的个人几点总结
- 如何提升程序员的“性能”
- 如何提升程序员的“性能”
- 如何提升程序员的“性能”
- 如何提升网站的性能?
- 如何处理Ibatis结合MySQL数据库使用时的事务操作
- infile.getline() infile.get() 出现的问题,还没有解决
- 一直困扰我的问题终于解决了,感谢Amanda2007博主
- 很好用的几个webServer服务,网上搜的
- CARP(Composition/Aggregation Reuse Principle)合成/聚合复用原则
- ubuntu 9.10 安装 vim latex-suite
- 使用Spring+Ibatis的项目,关于如何使用事务、如何提升性能的几点总结
- FORK()函数的理解
- Certinside提供HP0-S18考题下载
- 搞明白Linux下程序的内存占用
- Birt报表三大引擎的启动
- 生鲜加工成品预设的计划成本价格差错是否会影响成本核算
- [转] h.264 的学习过程
- 大数相乘的C++实现
- 生鲜管理解决方案