gpt4 book ai didi

java - Junit测试导致Mysql元数据锁

转载 作者:行者123 更新时间:2023-11-30 01:18:15 25 4
gpt4 key购买 nike

我有以下测试方法(基于之前创建和工作的测试用例):

`公共(public)类 CommentGenericDaoHibernateImplTest { 私有(private)评论评论; 私有(private) ReqCandAssociation reqCandAssc; 私有(private) JobReq JOBREQ; 私有(private)候选人 CANDIDATE;

private ClassPathXmlApplicationContext ctx;
private GenericDao<Comment, Integer> dao;
private GenericDao<JobReq, Integer> reqDao;
private GenericDao<Candidate, Integer> candDao;
private GenericDao<ReqCandAssociation, Integer> reqCandDao;

@SuppressWarnings("unchecked")
@Before
public void setUp() {


JOBREQ = new JobReq("TTYL", "Title", "contract", "project", "laborCategory", "summary", "jobDescription", "status", "TTONumber", new Date(), new Date(), "location", "lead", "leadEmail", "reason", "duration", "recruiter", "openingType", new Date(), "f", "2");
CANDIDATE = new Candidate("candName", "email", "phoneNum", "title", "mainStatus", new Date(), new Date(), new Date(), "none", "skillsSummary", "costRate", "clearanceType", new Date(), "contingentHire", "submittingCompany", "employingCompany", "5");
reqCandAssc = new ReqCandAssociation(CANDIDATE, JOBREQ, "Hired");

comment = new Comment("CommentGenericDaoHibernateImplTest TEST COMMENT", new Date(), reqCandAssc);

ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

reqDao = (GenericDaoHibernateImpl<JobReq, Integer>) ctx.getBean("jobReqDao");
candDao = (GenericDaoHibernateImpl<Candidate, Integer>) ctx.getBean("candDao");
reqCandDao = (GenericDaoHibernateImpl<ReqCandAssociation, Integer>) ctx.getBean("reqCandDao");
dao = (GenericDaoHibernateImpl<Comment, Integer>) ctx.getBean("commentDao");

reqDao.create(JOBREQ);
candDao.create(CANDIDATE);
reqCandDao.create(reqCandAssc);


}

@Test
public void testCreatingComment() {
try {
Integer key = reqDao.create(comment);
assertTrue("attachment not created successfully!", key > 0);
} finally {
if (ctx != null) {
ctx.close();
}
}
}

@Test
public void testRetrievingCommentByIdAfterCreate() {
try {
Integer key = dao.create(comment);
Comment retrievedComment = dao.read(key);
assertEquals("Could not find attachment with id = "+key, retrievedComment, comment);
} finally {
if (ctx != null) {
ctx.close();
}
}
}

@Test
public void testUpdatingCommentAfterCreate() {
try {

Integer key = dao.create(comment);
comment.setComment("ALTERED");
dao.update(comment);
Comment retrievedComment = dao.read(key);
assertEquals("Could not update attachment with id = "+key,
comment.getComment(), retrievedComment.getComment());
} finally {
if (ctx != null) {
ctx.close();
}
}
}

@Test
public void testDeletingCommentAfterCreate() {
try {

comment.setCommentDate(new Date());
Integer key = dao.create(comment);
assertTrue("attachment not created successfully!", key > 0);

dao.delete(comment);

Comment queriedComment = dao.read(key);
assertNull("Could not delete attachment with id = "+key, queriedComment);
} finally {
if (ctx != null) {
ctx.close();
}
}
}

@Test
public void testRetrievingAllCommentsAfterCreate() {
try {
Integer key = dao.create(comment);
assertTrue("attachment not created successfully!", key > 0);

List<Comment> queriedAttachmentList = dao.findAll();
assertEquals("Could not list all attachment", 1, queriedAttachmentList.size());
} finally {
if (ctx != null) {
ctx.close();
}
}
}

}`

在这样的方法中使用hibernate:

@SuppressWarnings("未选中")
公共(public) PK 创建(T o) {
session hibernateSession = this.getSession();
PK ID = 空;
尝试 {
hibernateSession.beginTransaction();
id = (PK) hibernateSession.save(o);
hibernateSession.getTransaction().commit();
} 最后 {
hibernateSession.close();
}
返回ID;
}

根据数据库采取行动。现在,Comment 实体位于具有 ReqCandAssociation 的 OneToMany 中,而 ReqCandAssociation 又是 JobReq 和 Candidate 的 ManyToMany 解析表。所以基本上我需要创建一个 JobReq、Candidate,然后我可以创建一个 ReqCandAssociation,然后添加一条注释附加到一个 ReqCandAssociation 对象。然而,每当我尝试向数据库添加多个对象时,MySQL 中的进程就会出现等待表元数据锁

我尝试过测试一次创建一个实体,它允许我创建一个候选人或职位申请并添加职位申请(而不是评论,用于测试),但是一旦我尝试添加候选人和工作req 然后做其他事情我得到了锁。我还尝试在 @Test 方法本身而不是设置中添加这些先决条件对象。

最佳答案

经过大量挖掘后发现,如果您的测试抛出错误,它会锁定数据库,由于某种原因,这些 Junit 测试不会像普通类那样显示语法错误突出显示,并且(似乎)编译得很好。我的一个对象构造函数中有一个错误的值,导致了错误。

在大量战略性放置 System.out.println() 之后发现了这一点

关于java - Junit测试导致Mysql元数据锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18833875/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com