免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4621 | 回复: 0
打印 上一主题 下一主题

MyBatis 并发问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-03-14 07:26 |只看该作者 |倒序浏览
转自 http://www.javaeye.com/topic/948752

最近需要用 到 HSQLDB + MyBatis  做开发,官方文档说SqlSession范围最好是线程级别的,SqlSessionFactory最好是应用级别的,以是我就简单写了一段代码测试一下,看可不可以重新封装SqlSession,让用户每次取出的SqlSession都是当前线程中的SqlSession,不同线程就获取不同的SqlSession。代码如下:
  1. public static void main(String[] args) throws Exception {
  2.                 for (int i = 0; i < 8; i++) {
  3.                         new Thread(new Runnable() {
  4.                                 public void run() {
  5.                                         //获取单例的SqlSessionFactory
  6.                                         System.out.println(Sigleton.getSessionFactory());
  7.                                         SqlSession session = Sigleton.getSessionFactory().openSession();
  8.                                         try {
  9.                                                 StudentMapper mapper = session.getMapper(StudentMapper.class);
  10.                                                 //运行时候,mapper的调用在并发过程中出现了异常,加上同步可以解决
  11.                                                 //但是,这应该不是问题根本....
  12.                                                 System.out.println(mapper.selectStudent(51));
  13.                                         } catch (Exception e) {
  14.                                                 e.printStackTrace();
  15.                                         }finally{
  16.                                                 session.close();
  17.                                         }
  18.                                 }
  19.                         }).start();
  20.                 }
  21.         }
复制代码
StudentMapper是一个接口,有selectStudent 方法,对应的sql语句配置在相应的映射文件里,如下
<mapper namespace="mybatis.test.StudentMapper">
<select id="selectStudent" parameterType="int" resultType="mybatis.test.Student">
select * from student where id = #{id}
</select>
</mapper>

异常信息:
  1. java.lang.IllegalArgumentException: Mapped Statements collection already contains value for mybatis.test.StudentMapper.selectStudent
  2.         at org.apache.ibatis.session.Configuration$StrictMap.put(Configuration.java:576)
  3.         at org.apache.ibatis.session.Configuration$StrictMap.put(Configuration.java:551)
  4.         at org.apache.ibatis.session.Configuration.addMappedStatement(Configuration.java:376)
  5.         at org.apache.ibatis.builder.MapperBuilderAssistant.addMappedStatement(MapperBuilderAssistant.java:215)
  6.         at org.apache.ibatis.builder.xml.XMLStatementBuilder.parseStatementNode(XMLStatementBuilder.java:64)
  7.         at org.apache.ibatis.session.Configuration.parseStatementNodes(Configuration.java:513)
  8.         at org.apache.ibatis.session.Configuration.buildStatementsForNamespace(Configuration.java:502)
  9.         at org.apache.ibatis.session.Configuration.buildStatementsFromId(Configuration.java:467)
  10.         at org.apache.ibatis.session.Configuration.getMappedStatement(Configuration.java:391)
  11.         at org.apache.ibatis.binding.MapperMethod.setupCommandType(MapperMethod.java:160)
  12.         at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:48)
  13.         at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:37)
  14.         at $Proxy1.selectStudent(Unknown Source)
  15.         at mybatis.test.MyBatisTest$1.run(MyBatisTest.java:28)
  16.         at java.lang.Thread.run(Thread.java:619)
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP