NutzCN Logo
问答 多线程开发,表产生了死锁问题。见代码和异常。
发布于 3144天前 作者 mingkaili 2782 次浏览 复制 上一个帖子 下一个帖子
标签:

上一次帖子就说了这个问题,但是代码比较多比较乱。我重新写了一个例子,发现了问题。但是不知道怎么解决,不废话了上代码。
主方法
service.java

package com.mxlvniao.lvniao.bug;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import com.mxlvniao.entity.dao.Category;

@IocBean(name = "demoService")
public class DemoService {

	@Inject
	private Dao dao;
	
	public void testThread()
			throws Exception {

		dao.clear(Category.class);
		
		Executor executor = Executors.newCachedThreadPool();
		CountDownLatch doneSignal = new CountDownLatch(5);
		
		for (int i=0;i<5;i++) {
			DemoThread command = new DemoThread(doneSignal, dao, "线程"+i);
			executor.execute(command);
		}
		doneSignal.await();
	}

}

线程方法

package com.mxlvniao.lvniao.bug;

import java.util.concurrent.CountDownLatch;

import org.nutz.dao.Dao;
import org.nutz.log.Log;
import org.nutz.log.Logs;

import com.mxlvniao.common.util.Util;
import com.mxlvniao.entity.dao.Category;

public class DemoThread implements Runnable {
	private static final Log log = Logs.get();

	private CountDownLatch doneSignal;
	private Dao dao;
	private String threadName;

	public DemoThread(CountDownLatch doneSignal, Dao dao, String threadName) {
		this.doneSignal = doneSignal;
		this.dao = dao;
		this.threadName = threadName;
	}

	@Override
	public void run() {
		try {
			log.debug("线程Name:" + threadName + "******开始*******");
			Category category = new Category();
			category.setId(Util.getUUID());
			category.setName("测试");
			dao.insert(category);
			log.debug("线程Name:" + threadName + "******结束*******");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (doneSignal != null) {
				doneSignal.countDown();
			}
		}
	}

}

POJO

package com.mxlvniao.entity.dao;

import java.io.Serializable;
import java.util.List;

import org.nutz.dao.entity.annotation.Column;
import org.nutz.dao.entity.annotation.Many;
import org.nutz.dao.entity.annotation.Name;
import org.nutz.dao.entity.annotation.Table;

@Table("tb_category")
public class Category implements Serializable {
	private static final long serialVersionUID = 7892928439318037192L;

	@Name
	private String id;
	@Column
	private String name;
	@Column
	private String pid;
	@Column
	private int qizhouId;
	@Many(target = Category.class, field = "pid")
	private List<Category> children;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	public int getQizhouId() {
		return qizhouId;
	}

	public void setQizhouId(int qizhouId) {
		this.qizhouId = qizhouId;
	}

	public List<Category> getChildren() {
		return children;
	}

	public void setChildren(List<Category> children) {
		this.children = children;
	}

}

异常:
2016-05-13 16:15:55,186 [org.nutz.mvc.impl.UrlMappingImpl:org.nutz.mvc.impl.UrlMappingImpl.get(UrlMappingImpl.java:92)] Found mapping for [GET] path=/demoAction : DemoAction.downloadQizhouData(DemoAction.java:18)
2016-05-13 16:15:55,191 [org.nutz.ioc.impl.NutIoc:org.nutz.ioc.impl.NutIoc.get(NutIoc.java:153)] Get 'demoAction'<class com.mxlvniao.lvniao.bug.DemoAction>
2016-05-13 16:15:55,192 [org.nutz.ioc.impl.NutIoc:org.nutz.ioc.impl.NutIoc.get(NutIoc.java:173)] >> Load definition name=demoAction
2016-05-13 16:15:55,194 [org.nutz.ioc.loader.combo.ComboIocLoader:org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:149)] Found IocObject(demoAction) in IocLoader(AnnotationIocLoader@2035161958)
2016-05-13 16:15:55,195 [org.nutz.ioc.impl.NutIoc:org.nutz.ioc.impl.NutIoc.get(NutIoc.java:203)] >> Make...'demoAction'<class com.mxlvniao.lvniao.bug.DemoAction>
2016-05-13 16:15:55,196 [org.nutz.ioc.impl.NutIoc:org.nutz.ioc.impl.NutIoc.get(NutIoc.java:153)] Get '$aop'<interface org.nutz.ioc.aop.config.AopConfigration>
2016-05-13 16:15:55,197 [org.nutz.ioc.impl.NutIoc:org.nutz.ioc.impl.NutIoc.get(NutIoc.java:173)] >> Load definition name=$aop
2016-05-13 16:15:55,197 [org.nutz.ioc.loader.map.MapLoader:org.nutz.ioc.loader.map.MapLoader.load(MapLoader.java:67)] Loading define for name=$aop
2016-05-13 16:15:55,206 [org.nutz.ioc.loader.combo.ComboIocLoader:org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:149)] Found IocObject($aop) in IocLoader(JsonLoader@433858857)
2016-05-13 16:15:55,206 [org.nutz.ioc.impl.NutIoc:org.nutz.ioc.impl.NutIoc.get(NutIoc.java:203)] >> Make...'$aop'<interface org.nutz.ioc.aop.config.AopConfigration>
2016-05-13 16:15:55,207 [org.nutz.ioc.impl.ScopeContext:org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:59)] Save object '$aop' to [app]
2016-05-13 16:15:55,257 [org.nutz.ioc.impl.NutIoc:org.nutz.ioc.impl.NutIoc.get(NutIoc.java:153)] Get 'transaction'<interface org.nutz.aop.MethodInterceptor>
2016-05-13 16:15:55,257 [org.nutz.ioc.impl.NutIoc:org.nutz.ioc.impl.NutIoc.get(NutIoc.java:173)] >> Load definition name=transaction
2016-05-13 16:15:55,257 [org.nutz.ioc.loader.map.MapLoader:org.nutz.ioc.loader.map.MapLoader.load(MapLoader.java:67)] Loading define for name=transaction
2016-05-13 16:15:55,258 [org.nutz.ioc.loader.combo.ComboIocLoader:org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:149)] Found IocObject(transaction) in IocLoader(JsonLoader@433858857)
2016-05-13 16:15:55,259 [org.nutz.ioc.impl.NutIoc:org.nutz.ioc.impl.NutIoc.get(NutIoc.java:203)] >> Make...'transaction'<interface org.nutz.aop.MethodInterceptor>
2016-05-13 16:15:55,259 [org.nutz.ioc.impl.ScopeContext:org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:59)] Save object 'transaction' to [app]
2016-05-13 16:15:55,263 [org.nutz.aop.asm.AsmClassAgent:org.nutz.aop.asm.AsmClassAgent.(AsmClassAgent.java:29)] AsmClassAgent will define class in Version 50
2016-05-13 16:15:55,288 [org.nutz.ioc.impl.ScopeContext:org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:59)] Save object 'demoAction' to [app]
2016-05-13 16:15:55,289 [org.nutz.ioc.impl.NutIoc:org.nutz.ioc.impl.NutIoc.get(NutIoc.java:153)] Get 'demoService'<class com.mxlvniao.lvniao.bug.DemoService>
2016-05-13 16:15:55,289 [org.nutz.ioc.impl.NutIoc:org.nutz.ioc.impl.NutIoc.get(NutIoc.java:173)] >> Load definition name=demoService
2016-05-13 16:15:55,290 [org.nutz.ioc.loader.combo.ComboIocLoader:org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:149)] Found IocObject(demoService) in IocLoader(AnnotationIocLoader@2035161958)
2016-05-13 16:15:55,290 [org.nutz.ioc.impl.NutIoc:org.nutz.ioc.impl.NutIoc.get(NutIoc.java:203)] >> Make...'demoService'<class com.mxlvniao.lvniao.bug.DemoService>
2016-05-13 16:15:55,290 [org.nutz.ioc.impl.NutIoc:org.nutz.ioc.impl.NutIoc.get(NutIoc.java:153)] Get 'transaction'<interface org.nutz.aop.MethodInterceptor>
2016-05-13 16:15:55,292 [org.nutz.ioc.impl.ScopeContext:org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:59)] Save object 'demoService' to [app]
2016-05-13 16:15:55,293 [org.nutz.ioc.impl.NutIoc:org.nutz.ioc.impl.NutIoc.get(NutIoc.java:153)] Get 'dao'<interface org.nutz.dao.Dao>
2016-05-13 16:15:55,293 [org.nutz.ioc.impl.NutIoc:org.nutz.ioc.impl.NutIoc.get(NutIoc.java:173)] >> Load definition name=dao
2016-05-13 16:15:55,293 [org.nutz.ioc.loader.map.MapLoader:org.nutz.ioc.loader.map.MapLoader.load(MapLoader.java:67)] Loading define for name=dao
2016-05-13 16:15:55,299 [org.nutz.ioc.loader.combo.ComboIocLoader:org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:149)] Found IocObject(dao) in IocLoader(JsonLoader@433858857)
2016-05-13 16:15:55,299 [org.nutz.ioc.impl.NutIoc:org.nutz.ioc.impl.NutIoc.get(NutIoc.java:203)] >> Make...'dao'<interface org.nutz.dao.Dao>
2016-05-13 16:15:55,318 [org.nutz.ioc.aop.impl.DefaultMirrorFactory:org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:84)] Load class org.nutz.dao.impl.NutDao without AOP
2016-05-13 16:15:55,318 [org.nutz.ioc.impl.ScopeContext:org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:59)] Save object 'dao' to [app]
2016-05-13 16:15:55,322 [org.nutz.ioc.impl.NutIoc:org.nutz.ioc.impl.NutIoc.get(NutIoc.java:153)] Get 'dataSource'<>
2016-05-13 16:15:55,322 [org.nutz.ioc.impl.NutIoc:org.nutz.ioc.impl.NutIoc.get(NutIoc.java:173)] >> Load definition name=dataSource
2016-05-13 16:15:55,323 [org.nutz.ioc.loader.map.MapLoader:org.nutz.ioc.loader.map.MapLoader.load(MapLoader.java:67)] Loading define for name=dataSource
2016-05-13 16:15:55,333 [org.nutz.ioc.loader.combo.ComboIocLoader:org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:149)] Found IocObject(dataSource) in IocLoader(JsonLoader@433858857)
2016-05-13 16:15:55,334 [org.nutz.ioc.impl.NutIoc:org.nutz.ioc.impl.NutIoc.get(NutIoc.java:203)] >> Make...'dataSource'<>
2016-05-13 16:15:55,362 [org.nutz.ioc.aop.impl.DefaultMirrorFactory:org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:84)] Load class com.alibaba.druid.pool.DruidDataSource without AOP
2016-05-13 16:15:55,362 [org.nutz.ioc.impl.ScopeContext:org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:59)] Save object 'dataSource' to [app]
2016-05-13 16:15:55,385 [org.nutz.ioc.impl.NutIoc:org.nutz.ioc.impl.NutIoc.get(NutIoc.java:153)] Get 'propertiesProxyManager'<>
2016-05-13 16:15:55,385 [org.nutz.ioc.impl.NutIoc:org.nutz.ioc.impl.NutIoc.get(NutIoc.java:173)] >> Load definition name=propertiesProxyManager
2016-05-13 16:15:55,385 [org.nutz.ioc.loader.map.MapLoader:org.nutz.ioc.loader.map.MapLoader.load(MapLoader.java:67)] Loading define for name=propertiesProxyManager
2016-05-13 16:15:55,386 [org.nutz.ioc.loader.combo.ComboIocLoader:org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:149)] Found IocObject(propertiesProxyManager) in IocLoader(JsonLoader@433858857)
2016-05-13 16:15:55,386 [org.nutz.ioc.impl.NutIoc:org.nutz.ioc.impl.NutIoc.get(NutIoc.java:203)] >> Make...'propertiesProxyManager'<>
2016-05-13 16:15:55,386 [org.nutz.ioc.aop.impl.DefaultMirrorFactory:org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:84)] Load class com.mxlvniao.common.PropertiesProxyManager without AOP
2016-05-13 16:15:55,387 [org.nutz.ioc.impl.ScopeContext:org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:59)] Save object 'propertiesProxyManager' to [app]
2016-05-13 16:15:55,389 [org.nutz.resource.Scans:org.nutz.resource.Scans.scan(Scans.java:233)] Found 1 resource by src( jdbc.properties ) , regex( ^.+[.]properties$ )
2016-05-13 16:15:55,391 [org.nutz.resource.Scans:org.nutz.resource.Scans.scan(Scans.java:233)] Found 1 resource by src( version.properties ) , regex( ^.+[.]properties$ )
2016-05-13 16:15:55,397 [org.nutz.ioc.impl.NutIoc:org.nutz.ioc.impl.NutIoc.get(NutIoc.java:153)] Get 'propertiesProxyManager'<>
2016-05-13 16:15:55,397 [org.nutz.ioc.impl.NutIoc:org.nutz.ioc.impl.NutIoc.get(NutIoc.java:153)] Get 'propertiesProxyManager'<>
2016-05-13 16:15:55,397 [org.nutz.ioc.impl.NutIoc:org.nutz.ioc.impl.NutIoc.get(NutIoc.java:153)] Get 'propertiesProxyManager'<>
2016-05-13 16:15:55,399 [org.nutz.ioc.impl.NutIoc:org.nutz.ioc.impl.NutIoc.get(NutIoc.java:153)] Get 'dataSource'<>
2016-05-13 16:15:55,413 [org.nutz.dao.impl.NutDao:org.nutz.dao.impl.NutDao.(NutDao.java:107)] NutDao[_selfId=0] init ...
2016-05-13 16:15:55,438 [org.nutz.filepool.NutFilePool:org.nutz.filepool.NutFilePool.(NutFilePool.java:23)] Init file-pool by: C:\Users\Administrator/.nutz/tmp/dao/ [200000]
2016-05-13 16:15:55,440 [org.nutz.filepool.NutFilePool:org.nutz.filepool.NutFilePool.(NutFilePool.java:37)] file-pool.home: 'C:\Users\Administrator.nutz\tmp\dao'
2016-05-13 16:15:55,442 [org.nutz.filepool.NutFilePool:org.nutz.filepool.NutFilePool.(NutFilePool.java:66)] file-pool.cursor: 0
2016-05-13 16:15:55,458 [org.nutz.dao.jdbc.Jdbcs:org.nutz.dao.jdbc.Jdbcs.(Jdbcs.java:90)] Jdbcs init complete
2016-05-13 16:15:55,458 [org.nutz.dao.jdbc.Jdbcs:org.nutz.dao.jdbc.Jdbcs.getExpert(Jdbcs.java:103)] Get Connection from DataSource for JdbcExpert, if I lock at here, check your database server and configure
2016-05-13 16:15:55,518 [com.alibaba.druid.pool.DruidDataSource:com.alibaba.druid.pool.DruidDataSource.init(DruidDataSource.java:669)] {dataSource-1} inited
2016-05-13 16:15:55,738 [org.nutz.dao.impl.DaoSupport:org.nutz.dao.impl.DaoSupport$1.invoke(DaoSupport.java:183)] JDBC Driver --> mysql-connector-java-5.1.38 ( Revision: fe541c166cec739c74cc727c5da96c1028b4834a )
2016-05-13 16:15:55,739 [org.nutz.dao.impl.DaoSupport:org.nutz.dao.impl.DaoSupport$1.invoke(DaoSupport.java:184)] JDBC Name --> MySQL Connector Java
2016-05-13 16:15:55,739 [org.nutz.dao.impl.DaoSupport:org.nutz.dao.impl.DaoSupport$1.invoke(DaoSupport.java:186)] JDBC URL --> jdbc:mysql://127.0.0.1:3306/db_8084_website
2016-05-13 16:15:55,746 [org.nutz.dao.impl.DaoSupport:org.nutz.dao.impl.DaoSupport$1.invoke(DaoSupport.java:196)] Mysql : character_set_client=utf8
2016-05-13 16:15:55,747 [org.nutz.dao.impl.DaoSupport:org.nutz.dao.impl.DaoSupport$1.invoke(DaoSupport.java:196)] Mysql : character_set_connection=utf8
2016-05-13 16:15:55,747 [org.nutz.dao.impl.DaoSupport:org.nutz.dao.impl.DaoSupport$1.invoke(DaoSupport.java:196)] Mysql : character_set_database=utf8
2016-05-13 16:15:55,747 [org.nutz.dao.impl.DaoSupport:org.nutz.dao.impl.DaoSupport$1.invoke(DaoSupport.java:196)] Mysql : character_set_filesystem=binary
2016-05-13 16:15:55,748 [org.nutz.dao.impl.DaoSupport:org.nutz.dao.impl.DaoSupport$1.invoke(DaoSupport.java:196)] Mysql : character_set_results=
2016-05-13 16:15:55,748 [org.nutz.dao.impl.DaoSupport:org.nutz.dao.impl.DaoSupport$1.invoke(DaoSupport.java:196)] Mysql : character_set_server=utf8
2016-05-13 16:15:55,749 [org.nutz.dao.impl.DaoSupport:org.nutz.dao.impl.DaoSupport$1.invoke(DaoSupport.java:196)] Mysql : character_set_system=utf8
2016-05-13 16:15:55,749 [org.nutz.dao.impl.DaoSupport:org.nutz.dao.impl.DaoSupport$1.invoke(DaoSupport.java:196)] Mysql : character_sets_dir=C:\Program Files\MySQL\MySQL Server 5.5\share\charsets\
2016-05-13 16:15:55,750 [org.nutz.dao.impl.DaoSupport:org.nutz.dao.impl.DaoSupport$1.invoke(DaoSupport.java:203)] Mysql : database=db_8084_website
2016-05-13 16:15:55,751 [org.nutz.dao.impl.DaoSupport:org.nutz.dao.impl.DaoSupport$1.invoke(DaoSupport.java:209)] Mysql : user=root@localhost
2016-05-13 16:15:55,784 [org.nutz.dao.impl.DaoSupport:org.nutz.dao.impl.DaoSupport.setDataSource(DaoSupport.java:224)] Database info --> MYSQL:[MySQL - 5.5.25]
2016-05-13 16:15:55,841 [org.nutz.dao.impl.sql.run.NutDaoExecutor:org.nutz.dao.impl.sql.run.NutDaoExecutor._runStatement(NutDaoExecutor.java:317)] DELETE FROM tb_category
2016-05-13 16:15:55,866 [org.nutz.dao.impl.sql.run.NutDaoExecutor:org.nutz.dao.impl.sql.run.NutDaoExecutor.exec(NutDaoExecutor.java:89)] SQLException
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (db_8084_website.tb_commodity, CONSTRAINT FK_Reference_3 FOREIGN KEY (cateId) REFERENCES tb_category (id))
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:932)
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:2547)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2505)
at com.mysql.jdbc.StatementImpl.executeInternal(StatementImpl.java:840)
at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:740)
at com.alibaba.druid.pool.DruidPooledStatement.execute(DruidPooledStatement.java:421)
at org.nutz.dao.impl.sql.run.NutDaoExecutor._runStatement(NutDaoExecutor.java:321)
at org.nutz.dao.impl.sql.run.NutDaoExecutor.exec(NutDaoExecutor.java:78)
at org.nutz.dao.impl.DaoSupport$DaoExec.invoke(DaoSupport.java:316)
at org.nutz.dao.impl.sql.run.NutDaoRunner.run(NutDaoRunner.java:38)
at org.nutz.dao.impl.DaoSupport.run(DaoSupport.java:239)
at org.nutz.dao.impl.DaoSupport$DaoExec.run(DaoSupport.java:306)
at org.nutz.trans.Trans.exec(Trans.java:174)
at org.nutz.dao.impl.DaoSupport._exec(DaoSupport.java:276)
at org.nutz.dao.impl.NutDao.clear(NutDao.java:622)
at org.nutz.dao.impl.NutDao.clear(NutDao.java:633)
at com.mxlvniao.lvniao.bug.DemoService.testThread(DemoService.java:25)
at com.mxlvniao.lvniao.bug.DemoService$$NUTZAOP._aop_invoke(DemoService.java:1)
at org.nutz.aop.InterceptorChain.invoke(InterceptorChain.java:77)
at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:57)
at org.nutz.aop.interceptor.TransactionInterceptor.filter(TransactionInterceptor.java:34)
at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:60)
at com.mxlvniao.lvniao.bug.DemoService$$NUTZAOP.testThread(DemoService.java:1)
at com.mxlvniao.lvniao.bug.DemoAction.downloadQizhouData(DemoAction.java:18)
at com.mxlvniao.lvniao.bug.DemoAction$$NUTZAOP._aop_invoke(DemoAction.java:1)
at org.nutz.aop.InterceptorChain.invoke(InterceptorChain.java:77)
at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:57)
at org.nutz.aop.interceptor.TransactionInterceptor.filter(TransactionInterceptor.java:34)
at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:60)
at com.mxlvniao.lvniao.bug.DemoAction$$NUTZAOP.downloadQizhouData(DemoAction.java:1)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.nutz.mvc.impl.processor.MethodInvokeProcessor.process(MethodInvokeProcessor.java:25)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:33)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.ActionFiltersProcessor.process(ActionFiltersProcessor.java:58)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:123)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44)
at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:196)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
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: 'DELETE FROM tb_category'
PreparedStatement:
'DELETE FROM tb_category'
CaseMessage=Cannot delete or update a parent row: a foreign key constraint fails (db_8084_website.tb_commodity, CONSTRAINT FK_Reference_3 FOREIGN KEY (cateId) REFERENCES tb_category (id))
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:38)
at org.nutz.dao.impl.DaoSupport.run(DaoSupport.java:239)
at org.nutz.dao.impl.DaoSupport$DaoExec.run(DaoSupport.java:306)
at org.nutz.trans.Trans.exec(Trans.java:174)
at org.nutz.dao.impl.DaoSupport._exec(DaoSupport.java:276)
at org.nutz.dao.impl.NutDao.clear(NutDao.java:622)
at org.nutz.dao.impl.NutDao.clear(NutDao.java:633)
at com.mxlvniao.lvniao.bug.DemoService.testThread(DemoService.java:25)
at com.mxlvniao.lvniao.bug.DemoService$$NUTZAOP._aop_invoke(DemoService.java:1)
at org.nutz.aop.InterceptorChain.invoke(InterceptorChain.java:77)
at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:57)
at org.nutz.aop.interceptor.TransactionInterceptor.filter(TransactionInterceptor.java:34)
at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:60)
at com.mxlvniao.lvniao.bug.DemoService$$NUTZAOP.testThread(DemoService.java:1)
at com.mxlvniao.lvniao.bug.DemoAction.downloadQizhouData(DemoAction.java:18)
at com.mxlvniao.lvniao.bug.DemoAction$$NUTZAOP._aop_invoke(DemoAction.java:1)
at org.nutz.aop.InterceptorChain.invoke(InterceptorChain.java:77)
at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:57)
at org.nutz.aop.interceptor.TransactionInterceptor.filter(TransactionInterceptor.java:34)
at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:60)
at com.mxlvniao.lvniao.bug.DemoAction$$NUTZAOP.downloadQizhouData(DemoAction.java:1)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.nutz.mvc.impl.processor.MethodInvokeProcessor.process(MethodInvokeProcessor.java:25)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:33)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.ActionFiltersProcessor.process(ActionFiltersProcessor.java:58)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:123)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44)
at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:196)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
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.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (db_8084_website.tb_commodity, CONSTRAINT FK_Reference_3 FOREIGN KEY (cateId) REFERENCES tb_category (id))
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:932)
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:2547)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2505)
at com.mysql.jdbc.StatementImpl.executeInternal(StatementImpl.java:840)
at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:740)
at com.alibaba.druid.pool.DruidPooledStatement.execute(DruidPooledStatement.java:421)
at org.nutz.dao.impl.sql.run.NutDaoExecutor._runStatement(NutDaoExecutor.java:321)
at org.nutz.dao.impl.sql.run.NutDaoExecutor.exec(NutDaoExecutor.java:78)
... 57 more

5 回复

请大神帮我分析一下,产生这个问题的原因。我知道死锁是因为主方法的dao.clear(Category.class);和线程的dao.insert(category);产生的死锁。为啥dao.clear(Category.class);没执行完成就进行下一步了啊?再问一下,dao里面的方法都是异步执行的吗?

这代码看着就蛋疼,为毛不作为实例变量

Executor executor = Executors.newCachedThreadPool();

@wendal 网上说这个newCachedThreadPool()方法就可以了,它是无参的。

作为实例变量

另外, 这里报的是主键冲突,不是死锁

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (db_8084_website.tb_commodity, CONSTRAINT FK_Reference_3 FOREIGN KEY (cateId) REFERENCES tb_category (id))
添加回复
请先登陆
回到顶部