今天来个简单的。
我们的数据库里有一张表,用来存放实时数据。有一个字段是主键,内容规定为系统时间戳,精确到毫秒级。
也就是类似这样。
INSERT INTO TABLENAME (F_IT_ID) VALUES (TO_CHAR(SYSTIMESTAMP,'YYYYDDMMHI24MISSFF'))
这个指令是由JDBC发出并执行的。现在遇到一个问题。因为数据库操作速度过快,而这个字段又是主键。于是呢……
报错了。违反唯一约束。
也就是说,对Oracle而言,同一毫秒内插入了两条数据。
那么,那么,要怎么解决呢?
我们的Java代码大致如下:
getConnection...
for (int i = 0;i < size;i++){
executeCommand...
}
closeConnection...
如何解决呢?
上述代码均为伪代码,请不要深究语法细节。从宏观的层面上说,应该怎么解决这个问题呢?