搞了很久都不知道为什么会出现数据库死锁,也不知道怎么解决,求各位牛人不吝赐教我写了一个定时器 定时读取某一固定目录的文件(xml格式)然后循环解读xml 并把xml中的部分字段拿出来放到实体中,插入数据库过程就是这么简单 ,用的是dom4j解析器,可是总是跑着跑着就发生了数据库死锁 ,搞不明白为什么代码如下(**是我注释掉了,涉密)private void readFiles(List<String> filecontents) throws Exception {
// TODO Auto-generated method stub
for (int i=0;i< filecontents.size();i+=2){
String messageType = filecontents.get(i).split("\\.")[1];
SAXReader saxReader = new SAXReader();
Document doc = saxReader.read(new ByteArrayInputStream(filecontents.get(i+1).getBytes()));
Element root = doc.getRootElement();
Element foo;
if (messageType.equalsIgnoreCase("***")||messageType.equalsIgnoreCase("***")){
Trtask tk = new Trtask();
for (Iterator k = root.elementIterator("***"); k.hasNext();) {
foo = (Element) k.next();
messageType = foo.elementText("***");
tk.creationTime = Timestamp.valueOf(foo.elementText("creationTime").replace("T", " "));
tk.originatorAddress = getXmlEnumLabel(foo.elementText("***").trim(), "***");
tk.messageID = foo.elementText("messageID");
tk.messageType = messageType;
}
for (Iterator j = root.elementIterator("FileBody");j.hasNext();){
foo = (Element) j.next();
tk.sid = SpeedIDUtil.getId();
tk.fileContent = filecontents.get(i+1);
}
dao.insertOrUpdate(tk);
}else if(messageType.equalsIgnoreCase("***")||messageType.equalsIgnoreCase("***")){
Gendata gd = new Gendata();
for (Iterator k = root.elementIterator("FileHeader"); k.hasNext();) {
foo = (Element) k.next();
System.out.println("头文件部分"+foo.elementText("messageType"));
messageType = foo.elementText("messageType");
gd.messageID = foo.elementText("messageID");
gd.messageType = foo.elementText("messageType");
gd.originatorAddress = foo.elementText("**");
gd.recipientAddress = foo.elementText("**");
gd.creationTime = Timestamp.valueOf(foo.elementText("creationTime").replace("T"," "));
}
for (Iterator j = root.elementIterator("FileBody");j.hasNext();){
foo = (Element) j.next();
gd.sid = SpeedIDUtil.getId();
gd.argumentOfPerigee = foo.elementText("**");
gd.ascendNode = foo.elementText("**");
gd.dataID = foo.elementText("dataID");
gd.eccentricity = foo.elementText("**");
gd.epochTime = Timestamp.valueOf(foo.elementText("epochTime").replace("T"," "));
gd.inclination = foo.elementText("**");
gd.meanAnomaly = foo.elementText("**");
gd.satellite = foo.elementText("**");
gd.semimajorAxis = foo.elementText("**");
}
dao.insertOrUpdate(gd);
}else{
CommonProcessFiles cpf = new CommonProcessFiles();
cpf.sid = SpeedIDUtil.getId();
for (Iterator k = root.elementIterator("FileHeader"); k.hasNext();) {
foo = (Element) k.next();
cpf.creationTime = Timestamp.valueOf(foo.elementText("creationTime").replace("T"," "));
cpf.fileContent = filecontents.get(i+1);
cpf.messageID = foo.elementText("messageID");
cpf.messageType = foo.elementText("messageType");
cpf.originatorAddress = foo.elementText("**");
cpf.recipientAddress = foo.elementText("**");
}
for (Iterator j = root.elementIterator("FileBody"); j.hasNext();) {
foo = (Element) j.next();
cpf.trPlanID = foo.elementText("trPlanID");
if (foo.elementText("dataType")!=null){
cpf.dataType = foo.elementText("dataType").trim();
}
}
dao.insertOrUpdate(cpf);
}
}
}
// TODO Auto-generated method stub
for (int i=0;i< filecontents.size();i+=2){
String messageType = filecontents.get(i).split("\\.")[1];
SAXReader saxReader = new SAXReader();
Document doc = saxReader.read(new ByteArrayInputStream(filecontents.get(i+1).getBytes()));
Element root = doc.getRootElement();
Element foo;
if (messageType.equalsIgnoreCase("***")||messageType.equalsIgnoreCase("***")){
Trtask tk = new Trtask();
for (Iterator k = root.elementIterator("***"); k.hasNext();) {
foo = (Element) k.next();
messageType = foo.elementText("***");
tk.creationTime = Timestamp.valueOf(foo.elementText("creationTime").replace("T", " "));
tk.originatorAddress = getXmlEnumLabel(foo.elementText("***").trim(), "***");
tk.messageID = foo.elementText("messageID");
tk.messageType = messageType;
}
for (Iterator j = root.elementIterator("FileBody");j.hasNext();){
foo = (Element) j.next();
tk.sid = SpeedIDUtil.getId();
tk.fileContent = filecontents.get(i+1);
}
dao.insertOrUpdate(tk);
}else if(messageType.equalsIgnoreCase("***")||messageType.equalsIgnoreCase("***")){
Gendata gd = new Gendata();
for (Iterator k = root.elementIterator("FileHeader"); k.hasNext();) {
foo = (Element) k.next();
System.out.println("头文件部分"+foo.elementText("messageType"));
messageType = foo.elementText("messageType");
gd.messageID = foo.elementText("messageID");
gd.messageType = foo.elementText("messageType");
gd.originatorAddress = foo.elementText("**");
gd.recipientAddress = foo.elementText("**");
gd.creationTime = Timestamp.valueOf(foo.elementText("creationTime").replace("T"," "));
}
for (Iterator j = root.elementIterator("FileBody");j.hasNext();){
foo = (Element) j.next();
gd.sid = SpeedIDUtil.getId();
gd.argumentOfPerigee = foo.elementText("**");
gd.ascendNode = foo.elementText("**");
gd.dataID = foo.elementText("dataID");
gd.eccentricity = foo.elementText("**");
gd.epochTime = Timestamp.valueOf(foo.elementText("epochTime").replace("T"," "));
gd.inclination = foo.elementText("**");
gd.meanAnomaly = foo.elementText("**");
gd.satellite = foo.elementText("**");
gd.semimajorAxis = foo.elementText("**");
}
dao.insertOrUpdate(gd);
}else{
CommonProcessFiles cpf = new CommonProcessFiles();
cpf.sid = SpeedIDUtil.getId();
for (Iterator k = root.elementIterator("FileHeader"); k.hasNext();) {
foo = (Element) k.next();
cpf.creationTime = Timestamp.valueOf(foo.elementText("creationTime").replace("T"," "));
cpf.fileContent = filecontents.get(i+1);
cpf.messageID = foo.elementText("messageID");
cpf.messageType = foo.elementText("messageType");
cpf.originatorAddress = foo.elementText("**");
cpf.recipientAddress = foo.elementText("**");
}
for (Iterator j = root.elementIterator("FileBody"); j.hasNext();) {
foo = (Element) j.next();
cpf.trPlanID = foo.elementText("trPlanID");
if (foo.elementText("dataType")!=null){
cpf.dataType = foo.elementText("dataType").trim();
}
}
dao.insertOrUpdate(cpf);
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货