简体   繁体   中英

java servlet inserting data into mysql db with composite key

I'm working on a servlet that needs to insert some data to the db table with a composite primary key consists of the userid, dataid and CURRENT_TIMESTAMP . however im getting the following error when executing the query

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry'13-7-2013-09-13 23:22:24' for key 'PRIMARY'

I think this is caused by the multiple insertion of rows to the same table in the same time, though with different dataid. Is there any solution to this problem? Should I cancel CURRENT_TIMESTAMP as a part of the primary key to do the trick or there are some other better workarounds?

Thanks a lot and appreciate for any help!

If it's a log table , it is recommended not to use a primary key. If you want to accelerate some search in this table, create the appropriate indexes.

If you need a primary key (for example, if you plan to use it with JPA), it would be best to use a number, eg

ID int AUTO_INCREMENT PRIMARY KEY

For example, log4j can insert each event log into a database using a org.apache.log4j.jdbc.JDBCAppender .

See also MySQL storage engine for a large log table .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM