2012-11-09 96 views
6

正如你我都知道的,關於這類錯誤信息有很多問題。關鍵'PRIMARY'的重複條目'...'

但我無法找到任何好的答案,因爲有這麼多的答案。

我有一個表格存儲nonce從客戶端發送。

但即使沒有具有完全相同主鍵的記錄,有時(偶爾)db也會抱怨重複的主鍵插入。

下面是JVM顯示的內容。

[#|2012-11-09T11:06:52.098+0900|WARNING|glassfish3.1.2|javax.enterprise.system.container.ejb.com.sun.ejb.containers|_ThreadID=236;_ThreadName=Thread-2;|EJB5184:A system exception occurred during an invocation on EJB Nonce2Bean, method: public java.lang.Object kr.co.ticomms.gameground.business.AbstractEntityFacade.persist(java.lang.Object)|#] 

[#|2012-11-09T11:06:52.099+0900|WARNING|glassfish3.1.2|javax.enterprise.system.container.ejb.com.sun.ejb.containers|_ThreadID=236;_ThreadName=Thread-2;|javax.ejb.EJBException 
     ... 
Caused by: javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException 
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'c8b4bdb84606fed0-c8b4bdb84606fed0_1352426820765_1880007534138556' for key 'PRIMARY' 
Error Code: 1062 
Call: INSERT INTO NONCE2 (NONCE, UDID, CREATED_DATE) VALUES (?, ?, ?) 
     bind => [3 parameters bound] 
Query: InsertObjectQuery(c8b4bdb84606fed0/c8b4bdb84606fed0_1352426820765_1880007534138556402) 
     ... 
     ... 29 more 
Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException 
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'c8b4bdb84606fed0-c8b4bdb84606fed0_1352426820765_1880007534138556' for key 'PRIMARY' 
Error Code: 1062 
Call: INSERT INTO NONCE2 (NONCE, UDID, CREATED_DATE) VALUES (?, ?, ?) 
     bind => [3 parameters bound] 
Query: InsertObjectQuery(c8b4bdb84606fed0/c8b4bdb84606fed0_1352426820765_1880007534138556402) 
     ... 
     ... 59 more 
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'c8b4bdb84606fed0-c8b4bdb84606fed0_1352426820765_1880007534138556' for key 'PRIMARY' 
|#] 

下面是mysql顯示的內容。

mysql> SHOW VARIABLES LIKE "%version%"; 
+-------------------------+-------------------------+ 
| Variable_name   | Value     | 
+-------------------------+-------------------------+ 
| protocol_version  | 10      | 
| version     | 5.1.62-0ubuntu0.10.04.1 | 
| version_comment   | (Ubuntu)    | 
| version_compile_machine | x86_64     | 
| version_compile_os  | debian-linux-gnu  | 
+-------------------------+-------------------------+ 
5 rows in set (0.00 sec) 

mysql> DESC NONCE2; 
+--------------+--------------+------+-----+-------------------+-------+ 
| Field  | Type   | Null | Key | Default   | Extra | 
+--------------+--------------+------+-----+-------------------+-------+ 
| CREATED_DATE | timestamp | NO |  | CURRENT_TIMESTAMP |  | 
| UDID   | varchar(255) | NO | PRI | NULL    |  | 
| NONCE  | varchar(255) | NO | PRI | NULL    |  | 
+--------------+--------------+------+-----+-------------------+-------+ 
3 rows in set (0.00 sec) 

mysql> SHOW CREATE TABLE NONCE2; 
+--------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 
| Table | Create Table                                                    | 
+--------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 
| NONCE2 | CREATE TABLE `NONCE2` (
    `CREATED_DATE` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, 
    `UDID` varchar(255) NOT NULL, 
    `NONCE` varchar(255) NOT NULL, 
    PRIMARY KEY (`UDID`,`NONCE`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 | 
+--------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 
1 row in set (0.00 sec) 

+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ 
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | 
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ 
| NONCE2 |   0 | PRIMARY |   1 | UDID  | A   |   7 |  NULL | NULL |  | BTREE  |   | 
| NONCE2 |   0 | PRIMARY |   2 | NONCE  | A   |   403 |  NULL | NULL |  | BTREE  |   | 
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ 
2 rows in set (0.00 sec) 

mysql> 

我嘗試刪除並重新創建表,但會出現同樣的問題。

我在GlassFish上使用JPA。

任何幫助?

----------------------------------------- UPDATE

我正在與JPA合作。

ID class。

public class NonceId implements Serializable { 

    public static NonceId newInstance(final String udid, final String nonce) { 
     if (udid == null) { 
      throw new IllegalArgumentException("null udid"); 
     } 
     if (nonce == null) { 
      throw new IllegalArgumentException("null nonce"); 
     } 
     final NonceId instance = new NonceId(); 
     instance.udid = udid; 
     instance.nonce = nonce; 
     return instance; 
    } 

    @Override 
    public int hashCode() { 
     int hash = 7; 
     hash = 23 * hash + (this.udid != null ? this.udid.hashCode() : 0); 
     hash = 23 * hash + (this.nonce != null ? this.nonce.hashCode() : 0); 
     return hash; 
    } 

    @Override 
    public boolean equals(Object obj) { 
     if (obj == null) { 
      return false; 
     } 
     if (getClass() != obj.getClass()) { 
      return false; 
     } 
     final NonceId other = (NonceId) obj; 
     if ((this.udid == null) ? (other.udid != null) : !this.udid.equals(other.udid)) { 
      return false; 
     } 
     if ((this.nonce == null) ? (other.nonce != null) : !this.nonce.equals(other.nonce)) { 
      return false; 
     } 
     return true; 
    } 

    @Override 
    public String toString() { 
     return udid + "/" + nonce; 
    } 

    private String udid; 

    private String nonce; 
} 

實體類。

@Entity 
@IdClass(NonceId.class) 
@Table(name = "NONCE2") 
@XmlTransient 
public class Nonce2 implements Serializable { 

    public static final int UDID_SIZE_MIN = 1; 
    public static final int UDID_SIZE_MAX = 255; 
    public static final int NONCE_SIZE_MIN = 1; 
    public static final int NONCE_SIZE_MAX = 255; 

    public static Nonce2 newInstance(final String udid, final String nonce) { 
     if (nonce == null) { 
      throw new NullPointerException("null value"); 
     } 
     final Nonce2 instance = new Nonce2(); 
     instance.udid = udid; 
     instance.nonce = nonce; 
     return instance; 
    } 

    public Date getCreatedDate() { 
     return createdDate; 
    } 

    public String getUdid() { 
     return udid; 
    } 

    public String getNonce() { 
     return nonce; 
    } 

    @PrePersist 
    protected void _PrePersist() { 
     createdDate = new Date(); 
    } 

    @Override 
    public String toString() { 
     return udid + "/" + nonce; 
    } 

    @Column(name = "CREATED_DATE", nullable = false, updatable = false) 
    @Temporal(TemporalType.TIMESTAMP) 
    @NotNull 
    private Date createdDate; 

    @Id 
    @Column(name = "UDID", nullable = false, updatable = false) 
    @NotNull 
    @Size(min = UDID_SIZE_MIN, max = UDID_SIZE_MAX) 
    private String udid; 

    @Id 
    @Column(name = "NONCE", nullable = false, updatable = false) 
    @NotNull 
    @Size(min = NONCE_SIZE_MIN, max = NONCE_SIZE_MAX) 
    private String nonce; 
} 

而在我的過濾器我做

@WebFilter(urlPatterns = {"/*"}) 
public class Filter_ implements Filter { 

    @Override 
    public void doFilter(final ServletRequest request, 
         final ServletResponse response, 
         final FilterChain chain) 
     throws IOException, ServletException { 

     // check whether nonce is already exist via em.find(); 

     chain.doFilter(request, response); 

     // store nonce via em.persist(); // EXCEPTION IS HERE 
     // THERE IS NO SUCH RECORD check direct SQL console. 
    } 
} 

我JPA提供商似乎執行該語句。

INSERT INTO NONCE2 (NONCE, UDID, CREATED_DATE) VALUES (?, ?, ?) 
+1

你有沒有想過問題是什麼?在使用JPA和唯一VARCHAR(255)列的項目中,我們遇到了類似的問題。看起來異常錯誤消息只顯示索引值的前64個字符。當我們直接在DB中插入一條類似的記錄時,我們沒有重複的鍵錯誤。在你的情況下,「c8b4bdb84606fed0-c8b4bdb84606fed0_1352426820765_1880007534138556」也是64個字符長。但它並不是你想要插入的完整值:最後缺少402。 – tvirtualw

+1

我們找到了解決我們問題的方法。默認情況下,MySQL整理不區分大小寫。我們試圖在我們的表格中插入的值具有相同的字符,有時它們僅由一個字母爲大寫字母,另一個字母爲小寫字母而不同。像'abcd'和'aBcd'一樣。使用默認排序規則會導致唯一的索引違規(我猜主鍵相同)。我們將其更改爲utf8_bin,然後它爲我們工作。 – tvirtualw

+1

我想nonce必須有與英國俚語中的標準定義不同的含義! – Strawberry

回答

3

您在表格中定義了複合主鍵。

可能是您嘗試在同一個會話中批量插入記錄。批次可能包含所述鍵列的重複條目。請檢查一下。

另請發表您的插入代碼樣本數據。

+0

也許apache-http-client的自動重試功能正在造成這個問題。我深入瞭解它。 –

相關問題