使用Spring+Ibatis的项目,关于如何使用事务、如何提升性能的几点总结
来源:互联网 发布:手机怎么找淘宝店铺 编辑:程序博客网 时间:2024/05/17 22:35
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数据库使用时的事务操作
- 虚拟机下redhat内核升级到2.6.16的成功经验
- asp.net中unsafe编译
- iostat来对linux硬盘IO性能进行了解
- 富人的錢和窮人的刀
- iptables详解
- 使用Spring+Ibatis的项目,关于如何使用事务、如何提升性能的几点总结
- Shell命令特殊字符
- [转载]MTK手机软件系统工程和配置简介(三)
- 好好应用
- Struts 2标签库
- 软件太臃肿了,如何解决?
- 关于工作与薪酬(写给职场新人)
- C++编程思想笔记(1001)。。
- Camera 图像处理原理分析- 抗噪 变焦 频闪 等