什么叫批处理?
批处理是指连接一次数据库,提交多条SQL语句,一起执行.效率比较高.
但是他如果抛出异常,你怎么判断是哪个SQL语句不对?????
我不知道怎么判断~~~~~~~
今天做OA系统的 及时消息系统
里面要进行批处理插入数据,,结果抛出异常,
因为我做的是无论抛出异常,或是不抛出异常,都要强行进行提交,
我原来想,那就重我抛出异常的后面,重新执行一次吧(因为存在数据外键约束关系,.我这么做,很合理)
但后来发现,无论什么时候抛出异常,总是最后一句!!!!
我想了半天,终于明白,原来批处理就是这个意思,也就是说:你一次提交N条数据去数据库,执行.
至于第几条语句有问题,这是无法查找的,至少在应用程序里面是无法查找的
除非在ORACLE里面返回一个可以提示你哪条语句有问题的SQL!
代码:
package
src.netmes.message;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import
java.awt.Frame;
import
java.sql.SQLException;
import
java.util.Date;
import
java.util.Enumeration;
import
java.util.HashMap;
import
java.util.Hashtable;
import
java.util.List;
import
java.util.Vector;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
import
com.ibatis.sqlmap.client.SqlMapClient;
import
org.springframework.orm.ibatis.SqlMapClientFactoryBean;
import
src.common.ServiceLocator;
import
src.common.iBatisDaoBase;
import
src.common.tools;
import
src.netmes.message.base.Message;
import
src.netmes.message.base.Receiver;
import
src.netmes.message.control.MessageErrorException;
import
src.netmes.message.control.MessageStoreThread;
import
src.netmes.message.control.PThreadList;;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
public
class
MessageDaoImp
extends
iBatisDaoBase
implements
MessageDaoIF
...
{
private static final String INSERTNEWMESSAGE1="MessageDao.insertNewMessage1";//插入及时消息
private static final String INSERTNEWMESSAGE2="MessageDao.insertNewMessage2";//插入定时消息
private static final String INSERTNEWRECEIVER="ReceiveDao.insertNewReceive";//插入接收人记录实体
private static final String GET_MESSAGE_BY_USERID="ReceiveDao.getMessageByUserID";//查找该用户的及时消息
private static final String ARRIF_MESSAGE_RECEIVE="ReceiveDao.affrimMessage";//确认收到消息
private static final String GET_AWAKE_MESSAGE="ReceiveDao.getAwakeMessageByUserID";//确认收到消息
private static final String GET_PRAMARY_KEY="MessageDao.getPrimaryKey";//获得一个消息序列
static int receivernum=0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
*插入新生成的消息,以及消息接收人
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public Long insert(Message message) ...{
Object key=null;
message.utfP();//解决中文问题
if(message.getMestype()==1)
key=super.insert(INSERTNEWMESSAGE1,message);
else
key=super.insert(INSERTNEWMESSAGE2,message);
return (Long)key;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
*插入接收人记录
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public Long insertNewReceive(Receiver receiver) ...{
Object key=super.insert(INSERTNEWRECEIVER,receiver);
return (Long) key;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public List getimmetMessage(Long userid) ...{
HashMap map=new HashMap();
map.put("userid",userid);
return super.queryForList(GET_MESSAGE_BY_USERID,map);
}
//确认收到某消息
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public void affirmGetMessage(long userid, Long messageid, Date time) ...{
HashMap map=new HashMap();
map.put("userid",userid);
map.put("messageid",messageid);
map.put("nowtime",time);
super.update(ARRIF_MESSAGE_RECEIVE,map);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public List getAwakeMESByUserId(Long userid) ...{
HashMap map=new HashMap();
map.put("userid",userid);
return super.queryForList(GET_AWAKE_MESSAGE,map);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public Long getPrimaryKey() ...{
Object key=super.queryForObject(GET_PRAMARY_KEY,null);
return (Long)key;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public void insertMEG2REV(Hashtable messagequeue, Vector receiverqueue) throws MessageErrorException, SQLException...{
SqlMapClient sqlmap=(SqlMapClient)ServiceLocator.findMyBean("sqlMapClient");
//这个变量用来记录当插入接收信息时出现数据不一致的现象的时候,截断接收信息连表,重新加入到持久化队列,防止信息丢失
//值得一提的是:插入消息不可能出现错误,只有插入接收信息的时候才产生外键依赖错误~
int i=0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
try...{
sqlmap.startTransaction();
sqlmap.startBatch();
Enumeration em=messagequeue.elements();
//插入及时消息
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
while(em.hasMoreElements())...{
Message message=(Message)em.nextElement();
Object Key=sqlmap.insert("MessageDao.insertNewMessage3",message);
}
//插入接收信息
int REVSIZE=receiverqueue.size();
receivernum=receivernum+REVSIZE;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
for(i=0;i<REVSIZE;i++)...{
Receiver re=(Receiver)receiverqueue.get(i);
// System.out.println("----"+re.getMessage().getPrimaryKey()+" asa "+re.getPrimaryKey()+" "+re.getAffirmtime());
// if(re.getAffirmtime()==null)
if(re.getAffirmtime()==null)
sqlmap.insert("ReceiveDao.insertNewReceive2",re);
else
sqlmap.insert("ReceiveDao.insertNewReceive3",re);
}
sqlmap.executeBatch();
System.out.println("sssssreceivernum====="+receivernum);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
}catch(Exception e)...{
//肯定是发生没有外键依赖错误的,提取后面没有插入的,重新生成线程,加入到持久化队列
// Vector vv=new Vector();
// int REVSIZE2=receiverqueue.size();
// duishi(REVSIZE2,i);
// for(;i<REVSIZE2;i++){
// vv.add(receiverqueue.get(i));
//
// }
//PThreadList.getPThreadList().startIT(new MessageStoreThread(new Hashtable(),vv));
//插入失败,重新加入到持久化队列,批处理的特点!!同志们那!
PThreadList.getPThreadList().startIT(new MessageStoreThread(new Hashtable(),receiverqueue));
throw new MessageErrorException("插入消息和接收人 线程 失败--"+tools.millsTODateString(System.currentTimeMillis()));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
}finally...{
sqlmap.commitTransaction();
sqlmap.endTransaction();
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
private static void duishi(int n,int i)...{
Frame f=new Frame("丢失:"+n+" i="+i+" timse:"+tools.millsTODateString(System.currentTimeMillis()));
f.setVisible(true);
f.show();
}
}