Oracle中的设置
多线程导入数据到Oracle中,如果是自己设置主键的值,那么肯定会遇到主键冲突的问题。例如线程A计算出的id为10(max(id) + 1),在A线程还没有完成导入时线程B用相同办法得到的id也是10,这时两个线程都请求插入数据时就会出现违反唯一约束条件的错误。
为了解决这个问题,我想到两种解决方法。
一种是自己设置一个类,在一开始计算出所有的id值存在一个数组中,用一个方法来每次获取一个id值返回给来索要的线程,用同步锁把这个方法锁起来,这样就可以保证每个线程获取的id值不一样。但是我想这样锁着方法肯定会影响速度,而且如果开多个程序同时导入的话会出现大问题。(个人水平有限,想到的方法缺陷肯定很大,因此不推荐用这种方法)
自己不管主键的增加,把这个任务交给Oracle来完成。Oracle端通过序列和触发器完成,在完成插入前,从序列中获取到下一个值设置成id。
1.设置序列
create sequence SEQ_TEST
increment by 1
start with 1
nomaxvalue
nocycle
定义好sequence后,你就可以用currVal,nextVal取得值:
CurrVal:返回 sequence的当前值
NextVal:增加sequence的值,然后返回增加后sequence值
2.设置触发器
create or replace trigger TRG_TEST
before insert on TEST
for each row
begin
select SEQ_TEST.nextval into :new.TEST_ID from dual;
end;
这个的意思就是在向test表插入数据前,用序列SEQ_TEST的下一个值代替test表的TEST_ID.
至此,多线程导入数据可能会出现的主键冲突问题得以解决。
Java代码
其实代码思想很简单,就是将原始数据分成多个部分,每个线程分工导入不同的部分。我要实现的功能是将Excel表中的数据导入到Oracle中,因此我只需要先读取整个Excel,得到总行数,然后设置每个线程负责部分的开始位置和结束位置。贴代码:
//每个线程的起始位置
int startFirst = 1;
int startSecond = Methods.totalRows / 3;
int startThird = Methods.totalRows / 3 + Methods.totalRows / 3;
//每个线程的步长
int stepFirst = startSecond - 1 - 1;
int stepSecond = startThird - startSecond - 1;
int stepThird = Methods.totalRows - startThird;
new Thread(new InsertThread(startFirst, stepFirst, Methods.orclConnectionFirst)).start();
new Thread(new InsertThread(startSecond, stepSecond, Methods.orclConnectionSecond)).start();
new Thread(new InsertThread(startThird, stepThird, Methods.orclConnectionThird)).start();
注意这里一定要一个线程对应一个connection,公用一个connection达不到提速效果。此处的InsertThread就是自己定义个一个实现Runnable接口的类,在重写的run方法里进行插入Oracle操作,贴代码:
public class InsertThread implements Runnable{
private int startRow;
private int step;
private Connection connection;
public InsertThread(int startRow, int step, Connection connection){
this.startRow = startRow;
this.step = step;
this.connection = connection;
}
@Override
public void run() {
// TODO Auto-generated method stub
...
//这里进行插入操作
...
}
}