gpt4 book ai didi

java - MySQLIntegrityConstraintViolationException 使应用程序崩溃

转载 作者:行者123 更新时间:2023-11-29 02:59:04 28 4
gpt4 key购买 nike

此应用程序,RomeCNN , 使用 Rome 1.5.0遍历 CNN 新闻提要并将 URL 保存到数据库中。数据库拒绝重复链接。

存在 MySQLIntegrityConstraintViolationException 类型的异常是可以的,这些插入应该被数据库拒绝。也许有更好的方法来避免重复,但现在这已经足够了。 (除非发生其他事情?我在想“如果链接已经存在,捕获异常,然后继续下一个”。)

为什么应用程序会崩溃?在违反完整性约束一定阈值后,连接断开?

run:
[java] [EL Info]: 2014-11-03 01:20:39.432--ServerSession(28432534)--EclipseLink, version: Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd
[java] [EL Info]: connection: 2014-11-03 01:20:40.173--ServerSession(28432534)--file:/home/thufir/NetBeansProjects/RomeCNN/build/classes/_RomeReaderPU login successful
[java] [EL Warning]: 2014-11-03 01:20:40.405--UnitOfWork(6261946)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
[java] Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'http://rss.cnn.com/~r/rss/cnn_topstories/~3/WD5Aw61It7M/nr-kenya' for key 'UNQ_links_0'
[java] Error Code: 1062
[java] Call: INSERT INTO links.links (created, link, status) VALUES (?, ?, ?)
[java] bind => [3 parameters bound]
[java] Query: InsertObjectQuery(romereader.Link[ id=null ])
[java] Exception in thread "main" javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
[java] Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'http://rss.cnn.com/~r/rss/cnn_topstories/~3/WD5Aw61It7M/nr-kenya' for key 'UNQ_links_0'
[java] Error Code: 1062
[java] Call: INSERT INTO links.links (created, link, status) VALUES (?, ?, ?)
[java] bind => [3 parameters bound]
[java] Query: InsertObjectQuery(romereader.Link[ id=null ])
[java] at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:157)
[java] at romereader.LinkJpaController.create(LinkJpaController.java:31)
[java] at romereader.Main.getLinks(Main.java:41)
[java] at romereader.Main.main(Main.java:21)
[java] Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
[java] Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'http://rss.cnn.com/~r/rss/cnn_topstories/~3/WD5Aw61It7M/nr-kenya' for key 'UNQ_links_0'
[java] Error Code: 1062
[java] Call: INSERT INTO links.links (created, link, status) VALUES (?, ?, ?)
[java] bind => [3 parameters bound]
[java] Query: InsertObjectQuery(romereader.Link[ id=null ])
[java] at org.eclipse.persistence.exceptions.DatabaseException.sqlException(DatabaseException.java:331)
[java] at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(DatabaseAccessor.java:900)
[java] at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeNoSelect(DatabaseAccessor.java:962)
[java] at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.basicExecuteCall(DatabaseAccessor.java:631)
[java] at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeCall(DatabaseAccessor.java:558)
[java] at org.eclipse.persistence.internal.sessions.AbstractSession.basicExecuteCall(AbstractSession.java:2002)
[java] at org.eclipse.persistence.sessions.server.ClientSession.executeCall(ClientSession.java:298)
[java] at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:242)
[java] at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:228)
[java] at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.insertObject(DatasourceCallQueryMechanism.java:377)
[java] at org.eclipse.persistence.internal.queries.StatementQueryMechanism.insertObject(StatementQueryMechanism.java:165)
[java] at org.eclipse.persistence.internal.queries.StatementQueryMechanism.insertObject(StatementQueryMechanism.java:180)
[java] at org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.insertObjectForWrite(DatabaseQueryMechanism.java:489)
[java] at org.eclipse.persistence.queries.InsertObjectQuery.executeCommit(InsertObjectQuery.java:80)
[java] at org.eclipse.persistence.queries.InsertObjectQuery.executeCommitWithChangeSet(InsertObjectQuery.java:90)
[java] at org.eclipse.persistence.internal.queries.DatabaseQueryMechanism.executeWriteWithChangeSet(DatabaseQueryMechanism.java:301)
[java] at org.eclipse.persistence.queries.WriteObjectQuery.executeDatabaseQuery(WriteObjectQuery.java:58)
[java] at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:899)
[java] at org.eclipse.persistence.queries.DatabaseQuery.executeInUnitOfWork(DatabaseQuery.java:798)
[java] at org.eclipse.persistence.queries.ObjectLevelModifyQuery.executeInUnitOfWorkObjectLevelModifyQuery(ObjectLevelModifyQuery.java:108)
[java] at org.eclipse.persistence.queries.ObjectLevelModifyQuery.executeInUnitOfWork(ObjectLevelModifyQuery.java:85)
[java] at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2896)
[java] at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1804)
[java] at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1786)
[java] at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1737)
[java] at org.eclipse.persistence.internal.sessions.CommitManager.commitNewObjectsForClassWithChangeSet(CommitManager.java:226)
[java] at org.eclipse.persistence.internal.sessions.CommitManager.commitAllObjectsWithChangeSet(CommitManager.java:125)
[java] at org.eclipse.persistence.internal.sessions.AbstractSession.writeAllObjectsWithChangeSet(AbstractSession.java:4207)
[java] at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabase(UnitOfWorkImpl.java:1441)
[java] at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabaseWithChangeSet(UnitOfWorkImpl.java:1531)
[java] at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.commitRootUnitOfWork(RepeatableWriteUnitOfWork.java:277)
[java] at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitAndResume(UnitOfWorkImpl.java:1169)
[java] at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:132)
[java] ... 3 more
[java] Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'http://rss.cnn.com/~r/rss/cnn_topstories/~3/WD5Aw61It7M/nr-kenya' for key 'UNQ_links_0'
[java] at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
[java] at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
[java] at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
[java] at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
[java] at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
[java] at com.mysql.jdbc.Util.getInstance(Util.java:386)
[java] at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1040)
[java] at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
[java] at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
[java] at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2503)
[java] at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2664)
[java] at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2794)
[java] at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
[java] at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2458)
[java] at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2375)
[java] at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2359)
[java] at org.eclipse.persistence.internal.databaseaccess.DatabaseAccessor.executeDirectNoSelect(DatabaseAccessor.java:890)
[java] ... 34 more
[java] Java Result: 1

BUILD SUCCESSFUL
Total time: 6 seconds
thufir@dur:~/NetBeansProjects/RomeCNN$
thufir@dur:~/NetBeansProjects/RomeCNN$

JpaController:

package romereader;

import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import romereader.exceptions.NonexistentEntityException;


public class LinkJpaController implements Serializable {

public LinkJpaController(EntityManagerFactory emf) {
this.emf = emf;
}
private EntityManagerFactory emf = null;

public EntityManager getEntityManager() {
return emf.createEntityManager();
}

public void create(Link link) {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
em.persist(link);
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}

public void edit(Link link) throws NonexistentEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
link = em.merge(link);
em.getTransaction().commit();
} catch (Exception ex) {
String msg = ex.getLocalizedMessage();
if (msg == null || msg.length() == 0) {
Integer id = link.getId();
if (findLink(id) == null) {
throw new NonexistentEntityException("The link with id " + id + " no longer exists.");
}
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}

public void destroy(Integer id) throws NonexistentEntityException {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Link link;
try {
link = em.getReference(Link.class, id);
link.getId();
} catch (EntityNotFoundException enfe) {
throw new NonexistentEntityException("The link with id " + id + " no longer exists.", enfe);
}
em.remove(link);
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}

public List<Link> findLinkEntities() {
return findLinkEntities(true, -1, -1);
}

public List<Link> findLinkEntities(int maxResults, int firstResult) {
return findLinkEntities(false, maxResults, firstResult);
}

private List<Link> findLinkEntities(boolean all, int maxResults, int firstResult) {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
cq.select(cq.from(Link.class));
Query q = em.createQuery(cq);
if (!all) {
q.setMaxResults(maxResults);
q.setFirstResult(firstResult);
}
return q.getResultList();
} finally {
em.close();
}
}

public Link findLink(Integer id) {
EntityManager em = getEntityManager();
try {
return em.find(Link.class, id);
} finally {
em.close();
}
}

public int getLinkCount() {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
Root<Link> rt = cq.from(Link.class);
cq.select(em.getCriteriaBuilder().count(rt));
Query q = em.createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
} finally {
em.close();
}
}

}

虽然对数据库进行了插入,但我不清楚为什么它显然会因违反约束或特定 URL 而阻塞。

persistence.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="RomeReaderPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>romereader.Links</class>
<class>romereader.Link</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/links?zeroDateTimeBehavior=convertToNull"/>
<property name="javax.persistence.jdbc.password" value="password"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.user" value="jdbc"/>
</properties>
</persistence-unit>
</persistence>

最佳答案

最简单的方法是首先检查链接是否存在于数据库中,然后更新/跳过或插入新项目。基本上是这样的:

em = getEntityManager();
Link existing = em.find(Link.class, link.getLink());
if (existing == null) {
em.getTransaction().begin();
em.persist(link);
em.getTransaction().commit();
}

为了让它更健壮,我会使用 SyndEntry.getUri()值作为唯一标识符。我现在无法检查,但我确定它映射到 guid项目的元素:

<item>
<title>Protectionist wins Melbourne Cup</title>
<guid>http://edition.cnn.com/2014/11/04/asia/gallery/melbourne-cup/index.html</guid>
<link>http://edition.cnn.com/2014/11/04/asia/gallery/melbourne-cup/index.html?eref=edition</link>
<description>Australia's Melbourne Cup</description>
<pubDate>Tue, 04 Nov 2014 01:52:42 EST</pubDate>
</item>

如果您选择更新元素,那么 pubDate值可用于确定项目自上次保存后是否已更新。

RSS Duplicate Detection是一篇关于这个主题的有趣博客文章(不幸的是,目前只能通过 archive.org 找到。)

如果您阅读 RSS 2.0 specification ,你会发现 <link>可以从 <item> 中省略所以记住这一点也很好。

关于java - MySQLIntegrityConstraintViolationException 使应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26711486/

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