异常如下:
2016-05-12 19:22:27,067 [org.nutz.dao.impl.sql.run.NutDaoExecutor:org.nutz.dao.impl.sql.run.NutDaoExecutor._runPreparedStatement(NutDaoExecutor.java:254)] INSERT INTO mv_qizhou_search(goodsId,name,marketPrice,shopPrice,goodsType,cateId,destId,lastUpdate,goodsBrief,imgThumb,imgUrl,imgSmall) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
|------|----------------------|----|-------|----|----|-----|---------------------|--------------------------|-----------------------------------------------------------------------|------------------------------------------------------------------------|-----------------------------------------------------------------------|
| 6711 | 拉斯维加斯西峡谷直升机之旅(直升机往返) | ¥0 | ¥3309 | 34 | 20 | 332 | 2016-05-12 17:01:00 | 从拉斯维加斯乘坐直升机出发,一路看风景到达西峡谷 | http://res.7zhou.com/images/201605/goods_img/6711_G_1462981309630.jpg | http://res.7zhou.com/images/201605/source_img/6711_G_1462981309739.jpg | http://res.7zhou.com/images/201605/goods_img/6711_G_1462981309630.jpg |
For example:> "INSERT INTO mv_qizhou_search(goodsId,name,marketPrice,shopPrice,goodsType,cateId,destId,lastUpdate,goodsBrief,imgThumb,imgUrl,imgSmall) VALUES(6711,'拉斯维加斯西峡谷直升机之旅(直升机往返)','¥0','¥3309',34,20,332,'2016-05-12 17:01:00','从拉斯维加斯乘坐直升机出发,一路看风景到达西峡谷','http://res.7zhou.com/images/201605/goods_img/6711_G_1462981309630.jpg','http://res.7zhou.com/images/201605/source_img/6711_G_1462981309739.jpg','http://res.7zhou.com/images/201605/goods_img/6711_G_1462981309630.jpg') "
2016-05-12 19:23:17,126 [org.nutz.dao.impl.sql.run.NutDaoExecutor:org.nutz.dao.impl.sql.run.NutDaoExecutor.exec(NutDaoExecutor.java:89)] SQLException
com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Lock wait timeout exceeded; try restarting transaction
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.Util.getInstance(Util.java:387)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2478)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2625)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2551)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1192)
at com.alibaba.druid.pool.DruidPooledPreparedStatement.execute(DruidPooledPreparedStatement.java:493)
at org.nutz.dao.impl.sql.run.NutDaoExecutor._runPreparedStatement(NutDaoExecutor.java:268)
at org.nutz.dao.impl.sql.run.NutDaoExecutor.exec(NutDaoExecutor.java:82)
at org.nutz.dao.impl.DaoSupport$DaoExec.invoke(DaoSupport.java:316)
at org.nutz.dao.impl.sql.run.NutDaoRunner.run(NutDaoRunner.java:59)
at org.nutz.dao.impl.DaoSupport.run(DaoSupport.java:239)
at org.nutz.dao.impl.DaoSupport._exec(DaoSupport.java:271)
at org.nutz.dao.impl.EntityOperator.exec(EntityOperator.java:50)
at org.nutz.dao.impl.NutDao.insert(NutDao.java:158)
at com.mxlvniao.lvniao.qizhou.thread.QizhouDownloadThread.insertObj(QizhouDownloadThread.java:123)
at com.mxlvniao.lvniao.qizhou.thread.QizhouDownloadThread.run(QizhouDownloadThread.java:79)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
org.nutz.dao.DaoException: !Nutz SQL Error: 'INSERT INTO mv_qizhou_search(goodsId,name,marketPrice,shopPrice,goodsType,cateId,destId,lastUpdate,goodsBrief,imgThumb,imgUrl,imgSmall) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
|------|----------------------|----|-------|----|----|-----|---------------------|--------------------------|-----------------------------------------------------------------------|------------------------------------------------------------------------|-----------------------------------------------------------------------|
| 6711 | 拉斯维加斯西峡谷直升机之旅(直升机往返) | ¥0 | ¥3309 | 34 | 20 | 332 | 2016-05-12 17:01:00 | 从拉斯维加斯乘坐直升机出发,一路看风景到达西峡谷 | http://res.7zhou.com/images/201605/goods_img/6711_G_1462981309630.jpg | http://res.7zhou.com/images/201605/source_img/6711_G_1462981309739.jpg | http://res.7zhou.com/images/201605/goods_img/6711_G_1462981309630.jpg |
For example:> "INSERT INTO mv_qizhou_search(goodsId,name,marketPrice,shopPrice,goodsType,cateId,destId,lastUpdate,goodsBrief,imgThumb,imgUrl,imgSmall) VALUES(6711,'拉斯维加斯西峡谷直升机之旅(直升机往返)','¥0','¥3309',34,20,332,'2016-05-12 17:01:00','从拉斯维加斯乘坐直升机出发,一路看风景到达西峡谷','http://res.7zhou.com/images/201605/goods_img/6711_G_1462981309630.jpg','http://res.7zhou.com/images/201605/source_img/6711_G_1462981309739.jpg','http://res.7zhou.com/images/201605/goods_img/6711_G_1462981309630.jpg') "'
PreparedStatement:
'INSERT INTO mv_qizhou_search(goodsId,name,marketPrice,shopPrice,goodsType,cateId,destId,lastUpdate,goodsBrief,imgThumb,imgUrl,imgSmall) VALUES(?,?,?,?,?,?,?,?,?,?,?,?) '
CaseMessage=Lock wait timeout exceeded; try restarting transaction
at org.nutz.dao.impl.sql.run.NutDaoExecutor.exec(NutDaoExecutor.java:96)
at org.nutz.dao.impl.DaoSupport$DaoExec.invoke(DaoSupport.java:316)
at org.nutz.dao.impl.sql.run.NutDaoRunner.run(NutDaoRunner.java:59)
at org.nutz.dao.impl.DaoSupport.run(DaoSupport.java:239)
at org.nutz.dao.impl.DaoSupport._exec(DaoSupport.java:271)
at org.nutz.dao.impl.EntityOperator.exec(EntityOperator.java:50)
at org.nutz.dao.impl.NutDao.insert(NutDao.java:158)
at com.mxlvniao.lvniao.qizhou.thread.QizhouDownloadThread.insertObj(QizhouDownloadThread.java:123)
at com.mxlvniao.lvniao.qizhou.thread.QizhouDownloadThread.run(QizhouDownloadThread.java:79)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Lock wait timeout exceeded; try restarting transaction
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.Util.getInstance(Util.java:387)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2478)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2625)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2551)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1192)
at com.alibaba.druid.pool.DruidPooledPreparedStatement.execute(DruidPooledPreparedStatement.java:493)
at org.nutz.dao.impl.sql.run.NutDaoExecutor._runPreparedStatement(NutDaoExecutor.java:268)
at org.nutz.dao.impl.sql.run.NutDaoExecutor.exec(NutDaoExecutor.java:82)
... 11 more
2016-05-12 19:23:17,135 [org.nutz.dao.impl.sql.run.NutDaoExecutor:org.nutz.dao.impl.sql.run.NutDaoExecutor._runPreparedStatement(NutDaoExecutor.java:254)] INSERT INTO mv_qizhou_search(goodsId,name,marketPrice,shopPrice,goodsType,cateId,destId,lastUpdate,goodsBrief,imgThumb,imgUrl,imgSmall) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
|------|----------------|------|------|----|----|-----|---------------------|-----------------------------------------------------|-----------------------------------------------------------------------|------------------------------------------------------------------------|-----------------------------------------------------------------------|
| 6704 | 的的喀喀湖2日游(普诺出发) | ¥710 | ¥589 | 34 | 20 | 726 | 2016-05-10 01:31:00 | 安第斯山脉文明的重要摇篮,印加和艾马拉人心中神话般的地方。花2天时光沉浸在这片湖中,探寻丰富的秘鲁历史 | http://res.7zhou.com/images/201605/goods_img/6704_G_1462784682188.jpg | http://res.7zhou.com/images/201605/source_img/6704_G_1462784682235.jpg | http://res.7zhou.com/images/201605/goods_img/6704_G_1462784682188.jpg |
For example:> "INSERT INTO mv_qizhou_search(goodsId,name,marketPrice,shopPrice,goodsType,cateId,destId,lastUpdate,goodsBrief,imgThumb,imgUrl,imgSmall) VALUES(6704,'的的喀喀湖2日游(普诺出发)','¥710','¥589',34,20,726,'2016-05-10 01:31:00','安第斯山脉文明的重要摇篮,印加和艾马拉人心中神话般的地方。花2天时光沉浸在这片湖中,探寻丰富的秘鲁历史','http://res.7zhou.com/images/201605/goods_img/6704_G_1462784682188.jpg','http://res.7zhou.com/images/201605/source_img/6704_G_1462784682235.jpg','http://res.7zhou.com/images/201605/goods_img/6704_G_1462784682188.jpg') "
代码如下:
主程序:
private void downloadQizhouSearch(JSONObject jsonAccessToken)
throws Exception {
// ThreadPoolExecutor threadPool = (ThreadPoolExecutor) Executors
// .newCachedThreadPool();
List<MvQizhouCategory> listCategory = dao.query(MvQizhouCategory.class,
null);
dao.clear(MvQizhouSearch.class);
for (int j = 0; j < listCategory.size(); j++) {
MvQizhouCategory mvQizhouCategory = listCategory.get(j);
JSONObject jsonPara = new JSONObject();
jsonPara.put("access_token", jsonAccessToken);
JSONObject filter = new JSONObject();
filter.put("category_id", mvQizhouCategory.getId());
jsonPara.put("filter", filter);
JSONObject pagination = new JSONObject();
pagination.put("count", 1);
pagination.put("page", 1);
jsonPara.put("pagination", pagination);
Map<String, Object> mapPara = new HashMap<String, Object>();
mapPara.put("json", jsonPara.toString());
String result = Http.post("http://open.7zhou.com/search", mapPara,
30000);
log.debug("name:" + mvQizhouCategory.getName()
+ "=======计算总数=======result:" + result);
JSONObject jsonResult = JSONObject.fromObject(result);
int total = jsonResult.getJSONObject("paginated").getInt("total");
int pageCount = total % 10000 > 0 ? total / 10000 + 1
: total / 10000;
log.debug("total=" + total + "-------" + "pageCount=" + pageCount);
CountDownLatch doneSignal = new CountDownLatch(pageCount);
Executor executor = Executors.newFixedThreadPool(5);
for (int page = 1; page <= pageCount; page++) {
String threadName = "name:" + mvQizhouCategory.getName()
+ "----" + "pageNo:" + page;
QizhouDownloadThread command = new QizhouDownloadThread(doneSignal, dao,
threadName, mvQizhouCategory, jsonAccessToken, page);
executor.execute(command);
}
doneSignal.await();
}
// threadPool.shutdown();
}
线程
package com.mxlvniao.lvniao.qizhou.thread;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.nutz.dao.Dao;
import org.nutz.http.Http;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import com.mxlvniao.entity.dao.mv.MvQizhouCategory;
import com.mxlvniao.entity.dao.mv.MvQizhouSearch;
public class QizhouDownloadThread implements Runnable {
private static final Log log = Logs.get();
private static final String QIZHOU_PIC_URL_OLD = "http://open.7zhou.com";
private static final String QIZHOU_PIC_URL_NEW = "http://res.7zhou.com";
private CountDownLatch doneSignal;
private Dao dao;
private String threadName;
private MvQizhouCategory mvQizhouCategory;
private JSONObject jsonAccessToken;
private int page;
public QizhouDownloadThread(CountDownLatch doneSignal, Dao dao,
String threadName, MvQizhouCategory mvQizhouCategory,
JSONObject jsonAccessToken, int page) {
this.doneSignal = doneSignal;
this.dao = dao;
this.threadName = threadName;
this.mvQizhouCategory = mvQizhouCategory;
this.jsonAccessToken = jsonAccessToken;
this.page = page;
}
@Override
public void run() {
try {
log.debug("线程Name:" + threadName + "******开始*******");
JSONObject jsonPara = new JSONObject();
jsonPara.put("access_token", jsonAccessToken);
JSONObject filter = new JSONObject();
filter.put("category_id", mvQizhouCategory.getId());
jsonPara.put("filter", filter);
JSONObject pagination = new JSONObject();
pagination.put("count", 10000);
pagination.put("page", page);
jsonPara.put("pagination", pagination);
Map<String, Object> mapPara = new HashMap<String, Object>();
mapPara.put("json", jsonPara.toString());
String result = Http.post("http://open.7zhou.com/search", mapPara,
60000);
JSONObject jsonResult = JSONObject.fromObject(result);
if (!jsonResult.containsKey("status")
|| jsonResult.getJSONObject("status").getInt("succeed") == 0) {
log.debug("线程Name:" + threadName
+ "==========查询结果不正确或为空=============");
return;
}
if (jsonResult.getJSONObject("status").getInt("succeed") == 1) {
JSONArray dataArr = jsonResult.getJSONArray("data");
for (int i = 0; i < dataArr.size(); i++) {
try {
JSONObject obj = dataArr.getJSONObject(i);
insertObj(obj);
} catch (Exception e) {
e.printStackTrace();
continue;
}
}
}
log.debug("线程Name:" + threadName + "******结束*******");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (doneSignal != null) {
doneSignal.countDown();
}
}
}
private void insertObj(JSONObject obj) throws Exception {
MvQizhouSearch mvQizhouSearch = new MvQizhouSearch();
mvQizhouSearch.setGoodsId(obj.getInt("goods_id"));
mvQizhouSearch.setName(obj.getString("name"));
mvQizhouSearch.setMarketPrice(obj.getString("market_price"));
mvQizhouSearch.setShopPrice(obj.getString("shop_price"));
mvQizhouSearch.setGoodsType(obj.getInt("goods_type"));
mvQizhouSearch.setCateId(mvQizhouCategory.getId());
mvQizhouSearch.setDestId(obj.getInt("dest_id"));
SimpleDateFormat longFormatterWithHyphen2 = new SimpleDateFormat(
"yyyy-MM-dd HH:mm");
Date lastUpdate = longFormatterWithHyphen2.parse(obj
.getString("last_update"));
if (lastUpdate == null)
return;
mvQizhouSearch.setLastUpdate(lastUpdate);
mvQizhouSearch.setGoodsBrief(obj.getString("goods_brief"));
mvQizhouSearch.setImgThumb(obj.getJSONObject("img").getString("thumb")
.replace(QIZHOU_PIC_URL_OLD, QIZHOU_PIC_URL_NEW));
mvQizhouSearch.setImgUrl(obj.getJSONObject("img").getString("url")
.replace(QIZHOU_PIC_URL_OLD, QIZHOU_PIC_URL_NEW));
mvQizhouSearch.setImgSmall(obj.getJSONObject("img").getString("small")
.replace(QIZHOU_PIC_URL_OLD, QIZHOU_PIC_URL_NEW));
dao.insert(mvQizhouSearch);
}
}