- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
此应用程序,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/
我有一个闭源升级应用程序,它将我的数据库从旧格式迁移到新格式(创建新表并将数据从旧表迁移到新表)。 应用程序崩溃并出现 MySQLIntegrityConstraintViolationExcepti
我将 JPA 与 Hibernate 和 MySQL 一起使用。 我想要的是捕获MySQLIntegrityConstraintViolationException来获取unicity约束违规,并显示
此应用程序,RomeCNN , 使用 Rome 1.5.0遍历 CNN 新闻提要并将 URL 保存到数据库中。数据库拒绝重复链接。 存在 MySQLIntegrityConstraintViolati
我正在开发一个 Web 应用程序,在我提交用户唯一的表单时, MySQLIntegrityConstraintViolationException Duplicate entry -'usernam
mapper: update db_logistics.table_inventory_material set surplusAmount= when i
我真的很难理解为什么会出现这个错误。我得到它指的是我的艺术家实体中的 GLOBAL_ID 字段。我想我一定错过了有关 JPA 内部运作方式的一些信息。让我们考虑这两个实体。 @Entity publi
我似乎无法捕获这个异常,Eclipse 告诉我它“无法解析为类型”。我知道这个异常意味着我正在尝试插入重复的键值,并且我想捕获它并让用户知道。 我是否缺少导入或 jar? 谢谢:) 最佳答案 您必须首
有没有办法在插入/删除期间禁用此功能? 情况:我有一个 masterDB 和一个library1DB 和library2DB。 library1DB 和library2DB 是两个独立的数据库,但具有
当我执行EntityManager.find()时,抛出以下异常: Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstrain
我四处搜寻,但找不到任何帮助。 我正在尝试创建一个用户,但有 2 个字段始终为空(名字和部门)。 GUI (html): Firstname:
我想从 studentinfo 和 studentmarks 这两个表中获取数据。 我将 Join 与 where 子句一起使用,但它会显示: ConstraintViolationException
使用 java.sql。 使用以下类对数据库对象执行操作: public class TaskBusinessLogic { private TaskDao taskDao = null;
我正在运行 Spring Batch 并使用 JdbcPagingItemReader .使用示例配置: 我收到与以下相关的错误: Column 'id' in order clause
将新对象保存到Users表时,我在此方法调用上遇到MySQLIntegrityConstraintViolationException。 sessionFactory.getCurrentSessio
我有家长 Filter实体和一个方向Listads作为 @OneToMany 的 child 关系。我尝试使用 Hibernate hql 删除超过一周的广告查询但得到: com.mysql.jdbc
我有两个名为 Qa.java 和 Answeres.java 的实体类 我的 Qa 实体由答案列表组成。 Qa.Java @Entity @Table(name = "qa") public clas
我的观点add.scala.html是这样的 因此,如果我添加一个产品,它会显示在同一屏幕上,并且我有一个删除按钮来删除产品。我的问题是,当我添加新产品时,它工作正常,但是当我在删除任何产品后添加一个
我有两个 java hibernate 实体: @Entity public class Match_soccer extends Match{ @Id @GeneratedValue
我有一个查找表“婚姻状况”和“雇员”表。 定义到员工中的外键,用于定义该员工的婚姻状况,此列接受空值(不是必需的)。 现在我正在使用 hibernate 和 spring mvc。问题是,我正在从下拉
这个问题已经被问过很多次了,但我没有找到任何好的答案,所以我会再问一次。 我有如下的父子单向关系: @Entity @Table(name = "PARENT") public class Paren
我是一名优秀的程序员,十分优秀!