论坛首页 入门技术论坛

ibatis下insert、update、delete操作单元测试通过,发布到tomcat下执行失败

浏览 17908 次
该帖已经被评为新手帖
作者 正文
   发表时间:2007-07-19  
奇怪的问题,折腾了我一上午,分析一下。
ibatis下执行insert、update、delete操作,单元测试通过,发布到tomcat下执行update、delete成功,而insert时却报错,出错信息如下,分析分析。

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: SqlMapClient operation; SQL [];  
--- The error occurred in com/nmgnews/fip/dao/ibatis/maps/FipInfoSort.xml. 
--- The error occurred while applying a parameter map. 
--- Check the insertInfoSort-InlineParameterMap. 
--- Check the statement (update failed). 
--- Cause: java.sql.SQLException: Duplicate entry '06' for key 1; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:  
--- The error occurred in com/nmgnews/fip/dao/ibatis/maps/FipInfoSort.xml. 
--- The error occurred while applying a parameter map. 
--- Check the insertInfoSort-InlineParameterMap. 
--- Check the statement (update failed). 
--- Cause: java.sql.SQLException: Duplicate entry '06' for key 1
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:487)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:440)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:75)


root cause

org.springframework.dao.DataIntegrityViolationException: SqlMapClient operation; SQL [];  
--- The error occurred in com/nmgnews/fip/dao/ibatis/maps/FipInfoSort.xml. 
--- The error occurred while applying a parameter map. 
--- Check the insertInfoSort-InlineParameterMap. 
--- Check the statement (update failed). 
--- Cause: java.sql.SQLException: Duplicate entry '06' for key 1; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:  
--- The error occurred in com/nmgnews/fip/dao/ibatis/maps/FipInfoSort.xml. 
--- The error occurred while applying a parameter map. 
--- Check the insertInfoSort-InlineParameterMap. 
--- Check the statement (update failed). 
--- Cause: java.sql.SQLException: Duplicate entry '06' for key 1
org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.translate(SQLErrorCodeSQLExceptionTranslator.java:246)
org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:196)
org.springframework.orm.ibatis.SqlMapClientTemplate.insert(SqlMapClientTemplate.java:367)
com.nmgnews.fip.dao.ibatis.SqlMapFipInfoSortDao.insertInfoSort(SqlMapFipInfoSortDao.java:30)
com.nmgnews.fip.web.spring.EditInfoSortController.handleRequest(EditInfoSortController.java:48)
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:857)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:792)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:475)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:440)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:75)


root cause

com.ibatis.common.jdbc.exception.NestedSQLException:  
--- The error occurred in com/nmgnews/fip/dao/ibatis/maps/FipInfoSort.xml. 
--- The error occurred while applying a parameter map. 
--- Check the insertInfoSort-InlineParameterMap. 
--- Check the statement (update failed). 
--- Cause: java.sql.SQLException: Duplicate entry '06' for key 1
com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeUpdate(GeneralStatement.java:91)
com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.insert(SqlMapExecutorDelegate.java:447)
com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.insert(SqlMapSessionImpl.java:82)
org.springframework.orm.ibatis.SqlMapClientTemplate$9.doInSqlMapClient(SqlMapClientTemplate.java:369)
org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:193)
org.springframework.orm.ibatis.SqlMapClientTemplate.insert(SqlMapClientTemplate.java:367)
com.nmgnews.fip.dao.ibatis.SqlMapFipInfoSortDao.insertInfoSort(SqlMapFipInfoSortDao.java:30)
com.nmgnews.fip.web.spring.EditInfoSortController.handleRequest(EditInfoSortController.java:48)
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:857)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:792)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:475)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:440)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:75)


note The full stack trace of the root cause is available in the Apache Tomcat/5.0.28 logs.

   发表时间:2007-07-19  
是驱动的问题吗?还是ibatis的版本问题?好生奇怪,一定是个弱智问题,朋友们顶顶吧。
0 请登录后投票
   发表时间:2007-07-19  
没人遇到这样的问题吗?难道真是弱智问题吗?
0 请登录后投票
   发表时间:2007-07-19  
Cause: java.sql.SQLException: Duplicate entry '06' for key 1


这里写的不是很清楚么,table里面某个unique key重复了。
0 请登录后投票
   发表时间:2007-07-19  
bigpanda 写道
Cause: java.sql.SQLException: Duplicate entry '06' for key 1


这里写的不是很清楚么,table里面某个unique key重复了。


呵呵,我也想知道为什么会重复?前面我已提到“单元测试通过,发布到tomcat下执行update、delete成功,而insert时却报错”。是不是执行了2遍insert操作?
0 请登录后投票
   发表时间:2007-07-19  
最让人奇怪的是单元测试通过,而发布web应用测试失败。junit中的代码与servlet中的代码没有任何区别,只是setXXX()数据来源不同,想不出出错的道理来?而且update、delete在两种测试环境下均通过,而insert只在junit中通过。
0 请登录后投票
   发表时间:2007-07-19  
你只发信息 别人又不知道你是怎么写的  你让别人怎么办   把所有可能都列出来 然后问你是哪一种??
0 请登录后投票
   发表时间:2007-07-19  
测试的时候是否对有关联的表插入过数据?可以的话先把表初始化一下。
0 请登录后投票
   发表时间:2007-07-20  
ddandyy 写道
你只发信息 别人又不知道你是怎么写的  你让别人怎么办   把所有可能都列出来 然后问你是哪一种??

前面一再提到dao测试通过,servlet下测试失败,写java也不是一天两天了,个人猜测很可能不是代码上的问题,而是驱动版本或配置上的问题,因此没有发代码。首先简单介绍一下程序的架构,此程序采用spring+ibatis+mysql。主要jar包spring-2.0.6.jar,mysql-connector-java-3.1.11-bin.jar,ibatis-2.3.0.677.jar,servlet-api-2.4.jar
数据库表:
/*==============================================================*/
/* Table: FIP_InfoSorts                                         */
/*==============================================================*/
create table FIP_InfoSorts
(
   SKey                           varchar(10)                    not null,
  ISKey                          varchar(4),   SName                          varchar(60)                    not null,
   Description                    varchar(512),
   ParentKey                      varchar(2),
   primary key (SKey)
)
comment = "信息分类"
type = InnoDB;

/*==============================================================*/
/* Index: Rel_Sort_Scheme_FK                                    */
/*==============================================================*/
create index Rel_Sort_Scheme_FK on FIP_InfoSorts
(
   ISKey
);

alter table FIP_InfoSorts add constraint FK_Rel_Sort_Scheme foreign key (ISKey)
      references FIP_Input_Schemes (ISKey) on delete restrict on update restrict;
注:虽然有外键引用(ISKey),这里设置允许为null。

ibatis配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd">

<sqlMap namespace="FipInfoSort">

  <typeAlias alias="infoSort" type="org.limojfip.domain.FipInfoSort"/>

  <select id="getInfoSort"
  parameterClass="java.lang.String"
  resultClass="infoSort">
    SELECT SKey, SName, ParentKey FROM fip_infosorts where SKey = #value#
  </select>

  <insert id="insertInfoSort">
    insert into FIP_InfoSorts (SKey, SName)
    values (#SKey:VARCHAR#, #SName#)
  </insert>

  <update id="updateInfoSort" parameterClass="org.limojfip.domain.FipInfoSort">
    update FIP_InfoSorts
<dynamic prepend="set">
    <isNotNull prepend="," property="isKey">
ISKey=#isKey#
</isNotNull>
<isNotNull prepend="," property="SName">
SName=#SName#
</isNotNull>
<isNotNull prepend="," property="description">
Description=#description#
</isNotNull>
<isNull prepend="," property="description">
Description=null
</isNull>
<isNotNull prepend="," property="parentKey">
ParentKey=#parentKey#
</isNotNull>
<isNull prepend="," property="parentKey">
ParentKey=null
</isNull>
</dynamic>
    where SKey = #SKey#
  </update>
 
  <delete id="deleteInfoSort" parameterClass="java.lang.String">
    delete from FIP_InfoSorts where SKey = #value#
  </delete>

</sqlMap>

junit测试用例:
package org.limojfip.dao.test;

import java.util.List;

import junit.framework.TestCase;

import org.limojfip.dao.FipInfoSortDao;
import org.limojfip.domain.FipInfoSort;

public class SqlMapFipInfoSortDaoTest extends TestCase {

FipInfoSortDao isDao;
protected void setUp() throws Exception {
isDao = TestFactory.getFipFacadeImpl().getInfoSortDao();
}

public void testInsertInfoSort() {
FipInfoSort is = new FipInfoSort();
is.setSKey("61");
is.setSName("test");
is.setDescription("test61");
is.setIsKey(null);
is.setParentKey(null);
isDao.insertInfoSort(is);
}

public void testModifyInfoSort() {
FipInfoSort is = isDao.getInfoSort("61");
is.setSName("test61");
is.setDescription("test");
is.setIsKey("03");
is.setParentKey("03");
isDao.modifyInfoSort(is);
}

public void testDeleteInfoSort() {
isDao.deleteInfoSort("61");
}

}
servlet程序:
package org.limojfip.web.spring;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import org.limojfip.domain.FipActionLog;
import org.limojfip.domain.FipInfoSort;
import org.limojfip.domain.FipInfomation;
import org.limojfip.domain.FipInput;
import org.limojfip.domain.FipInputValue;
import org.limojfip.domain.InputType;
import org.limojfip.domain.logic.FipFacade;

public class EditInfoSortController implements Controller {

private FipFacade fipFacade;

public void setFipFacade(FipFacade fipFacade) {
this.fipFacade = fipFacade;
}
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
String action = request.getParameter("action");
String sKey = request.getParameter("skey");
String sName = request.getParameter("sname");
String description = request.getParameter("description");
String parentKey = request.getParameter("parentkey");
if(parentKey != null && parentKey.length() == 0)
parentKey = null;
if ("3".equals(action)) {
// delete
this.fipFacade.getInfoSortDao().deleteInfoSort(sKey);
response.sendRedirect("ra_01_01.do");
return null;
} else {
FipInfoSort is = new FipInfoSort();
is.setSKey(sKey);
is.setSName(sName);
is.setDescription(description);
is.setParentKey(parentKey);
if ("1".equals(action)) {
// insert
this.fipFacade.getInfoSortDao().insertInfoSort(is);
} else {
// modify
this.fipFacade.getInfoSortDao().modifyInfoSort(is);
}
response.sendRedirect("ra_01_02.do?action" + action
+ "&skey=" + sKey);
return null;
}
}
}
0 请登录后投票
   发表时间:2007-09-18  
由于时间问题,最终换了hibernate,没有进一步查找问题原因。我想最直接的办法是引用ibatis、spring源工程,跟踪程序的执行。
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics