1共用一个连接就可以 2其实一样可以用lock实现,把数据库 *** 作写成一个函数,函数内加lock。c#会安排他们排队 比如 private static object privateObjectLock = new object(); public static xxoo() { lock(privateObjectLock) { //数据 *** 作语句 } } } 你在一个函数里实现数据库 *** 作。然后线程 *** 作数据库都调用他
//将数据库中的数据条数分段 public void division(){ //获取要导入的总的数据条数 String sql3="SELECT count() FROM [CMD][dbo][mycopy1]"; try { pss=consprepareStatement(sql3); rss=pssexecuteQuery(); while(rssnext()){ Systemoutprintln("总记录条数:"+rssgetInt(1)); sum=rssgetInt(1); } //每30000条记录作为一个分割点 if(sum>=30000){ n=sum/30000; residue=sum%30000; }else{ residue=sum; } Systemoutprintln(n+" "+residue); } catch (SQLException e) { // TODO Auto-generated catch block eprintStackTrace(); } }线程类public MyThread(int start,int end) { thisend=end; thisstart=start; Systemoutprintln("处理掉余数"); try { Systemoutprintln("--------"+ThreadcurrentThread()getName()+"------------"); ClassforName(SQLSERVERDRIVER); Systemoutprintln("加载sqlserver驱动"); cons = DriverManagergetConnection(CONTENTS,UNS,UPS); stas = conscreateStatement(); Systemoutprintln("连接SQLServer数据库成功!!"); Systemoutprintln("加载mysql驱动"); ClassforName(MYSQLDRIVER); con = DriverManagergetConnection(CONTENT,UN,UP); sta = concreateStatement(); // 关闭事务自动提交 consetAutoCommit(false); Systemoutprintln("连接mysql数据库成功!!"); } catch (Exception e) { eprintStackTrace(); } // TODO Auto-generated constructor stub } public ArrayList<Member> getAll(){ Member member; String sql1="select from (select row_number() over (order by pmcode) as rowNum," + " from [CMD][dbo][mycopy1]) as t where rowNum between "+start+" and "+end; try { Systemoutprintln("正在获取数据"); allmembers=new ArrayList(); rss=stasexecuteQuery(sql1); while(rssnext()){ member=new Member(); membersetAddress1(rssgetString("address1")); membersetBnpoints(rssgetString("bnpoints")); membersetDbno(rssgetString("dbno")); membersetExpiry(rssgetString("expiry")); membersetHispoints(rssgetString("hispoints")); membersetKypoints(rssgetString("kypoints")); membersetLevels(rssgetString("levels")); membersetNames(rssgetString("names")); membersetPmcode(rssgetString("pmcode")); membersetRemark(rssgetString("remark")); membersetSex(rssgetString("sex")); membersetTelephone(rssgetString("telephone")); membersetWxno(rssgetString("wxno")); membersetPmdate(rssgetString("pmdate")); allmembersadd(member); // Systemoutprintln(membergetNames()); } Systemoutprintln("成功获取sqlserver数据库数据!"); return allmembers; } catch (SQLException e) { // TODO Auto-generated catch block Systemoutprintln("获取sqlserver数据库数据发送异常!"); eprintStackTrace(); } try { rssclose(); stasclose(); } catch (SQLException e) { // TODO Auto-generated catch block eprintStackTrace(); } return null; } public void inputAll(ArrayList<Member> allmembers){ Systemoutprintln("开始向mysql中写入"); String sql2="insert into testmycopy2 values (,,,,,,,,,,,,,)"; try { ps=conprepareStatement(sql2); Systemoutprintln("-------------------------等待写入数据条数: "+allmemberssize()); for(int i=0;i<allmemberssize();i++){ pssetString(1, allmembersget(i)getPmcode()); pssetString(2, allmembersget(i)getNames()); //Systemoutprintln(allmembersget(i)getNames()); pssetString(3, allmembersget(i)getSex()); pssetString(4, allmembersget(i)getTelephone()); pssetString(5, allmembersget(i)getAddress1()); pssetString(6, allmembersget(i)getPmdate()); pssetString(7, allmembersget(i)getExpiry()); pssetString(8, allmembersget(i)getLevels()); pssetString(9, allmembersget(i)getDbno()); pssetString(10, allmembersget(i)getHispoints()); pssetString(11, allmembersget(i)getBnpoints()); pssetString(12, allmembersget(i)getKypoints()); pssetString(13, allmembersget(i)getWxno()); pssetString(14, allmembersget(i)getRemark()); //插入命令列表 //psaddBatch(); psexecuteUpdate(); } //psexecuteBatch(); concommit(); psclose(); conclose(); thisflag=false; Systemoutprintln(ThreadcurrentThread()getName()+"--->OK"); } catch (SQLException e) { // TODO Auto-generated catch block Systemoutprintln("向mysql中更新数据时发生异常!"); eprintStackTrace(); } } @Override public void run() { // TODO Auto-generated method stub while(true&&flag){ thisinputAll(getAll()); } }
//将数据库中的数据条数分段
public void division(){
//获取要导入的总的数据条数
String sql3="SELECT count() FROM [CMD][dbo][mycopy1]";
try {
pss=consprepareStatement(sql3);
rss=pssexecuteQuery();
while(rssnext()){
Systemoutprintln("总记录条数:"+rssgetInt(1));
sum=rssgetInt(1);
}
//每30000条记录作为一个分割点
if(sum>=30000){
n=sum/30000;
residue=sum%30000;
}else{
residue=sum;
}
Systemoutprintln(n+" "+residue);
} catch (SQLException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
}
线程类
public MyThread(int start,int end) {
thisend=end;
thisstart=start;
Systemoutprintln("处理掉余数");
try {
Systemoutprintln("--------"+ThreadcurrentThread()getName()+"------------");
ClassforName(SQLSERVERDRIVER);
Systemoutprintln("加载sqlserver驱动");
cons = DriverManagergetConnection(CONTENTS,UNS,UPS);
stas = conscreateStatement();
Systemoutprintln("连接SQLServer数据库成功!!");
Systemoutprintln("加载mysql驱动");
ClassforName(MYSQLDRIVER);
con = DriverManagergetConnection(CONTENT,UN,UP);
sta = concreateStatement();
// 关闭事务自动提交
consetAutoCommit(false);
Systemoutprintln("连接mysql数据库成功!!");
} catch (Exception e) {
eprintStackTrace();
}
// TODO Auto-generated constructor stub
}
public ArrayList<Member> getAll(){
Member member;
String sql1="select from (select row_number() over (order by pmcode) as rowNum," +
" from [CMD][dbo][mycopy1]) as t where rowNum between "+start+" and "+end;
try {
Systemoutprintln("正在获取数据");
allmembers=new ArrayList();
rss=stasexecuteQuery(sql1);
while(rssnext()){
member=new Member();
membersetAddress1(rssgetString("address1"));
membersetBnpoints(rssgetString("bnpoints"));
membersetDbno(rssgetString("dbno"));
membersetExpiry(rssgetString("expiry"));
membersetHispoints(rssgetString("hispoints"));
membersetKypoints(rssgetString("kypoints"));
membersetLevels(rssgetString("levels"));
membersetNames(rssgetString("names"));
membersetPmcode(rssgetString("pmcode"));
membersetRemark(rssgetString("remark"));
membersetSex(rssgetString("sex"));
membersetTelephone(rssgetString("telephone"));
membersetWxno(rssgetString("wxno"));
membersetPmdate(rssgetString("pmdate"));
allmembersadd(member);
// Systemoutprintln(membergetNames());
}
Systemoutprintln("成功获取sqlserver数据库数据!");
return allmembers;
} catch (SQLException e) {
// TODO Auto-generated catch block
Systemoutprintln("获取sqlserver数据库数据发送异常!");
eprintStackTrace();
}
try {
rssclose();
stasclose();
} catch (SQLException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
return null;
}
public void inputAll(ArrayList<Member> allmembers){
Systemoutprintln("开始向mysql中写入");
String sql2="insert into testmycopy2 values (,,,,,,,,,,,,,)";
try {
ps=conprepareStatement(sql2);
Systemoutprintln("-------------------------等待写入数据条数: "+allmemberssize());
for(int i=0;i<allmemberssize();i++){
pssetString(1, allmembersget(i)getPmcode());
pssetString(2, allmembersget(i)getNames());
//Systemoutprintln(allmembersget(i)getNames());
pssetString(3, allmembersget(i)getSex());
pssetString(4, allmembersget(i)getTelephone());
pssetString(5, allmembersget(i)getAddress1());
pssetString(6, allmembersget(i)getPmdate());
pssetString(7, allmembersget(i)getExpiry());
pssetString(8, allmembersget(i)getLevels());
pssetString(9, allmembersget(i)getDbno());
pssetString(10, allmembersget(i)getHispoints());
pssetString(11, allmembersget(i)getBnpoints());
pssetString(12, allmembersget(i)getKypoints());
pssetString(13, allmembersget(i)getWxno());
pssetString(14, allmembersget(i)getRemark());
//插入命令列表
//psaddBatch();
psexecuteUpdate();
}
//psexecuteBatch();
concommit();
psclose();
conclose();
thisflag=false;
Systemoutprintln(ThreadcurrentThread()getName()+"--->OK");
} catch (SQLException e) {
// TODO Auto-generated catch block
Systemoutprintln("向mysql中更新数据时发生异常!");
eprintStackTrace();
}
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true&&flag){
thisinputAll(getAll());
}
}
SQLite作为一款小型的嵌入式数据库,本身没有提供复杂的锁定机制,无法内部管理多路并发下的数据 *** 作同步问题,更谈不上优化,所以涉及到多路并发的情况,需要外部进行读写锁控制,否则SQLite会返回SQLITE_BUSY错误,以驳回相关请求。
返回SQLITE_BUSY主要有以下几种情况:
1。当有写 *** 作时,其他读 *** 作会被驳回
2。当有写 *** 作时,其他写 *** 作会被驳回
3。当开启事务时,在提交事务之前,其他写 *** 作会被驳回
4。当开启事务时,在提交事务之前,其他事务请求会被驳回
5。当有读 *** 作时,其他写 *** 作会被驳回
6。读 *** 作之间能够并发执行
基于以上讨论,可以看出这是一个典型的读者写者问题,读 *** 作要能够共享,写 *** 作要互斥,读写之间也要互斥
可以设计如下的方案解决并发 *** 作数据库被锁定的问题,同时保证读 *** 作能够保持最大并发
1。采用互斥锁控制数据库写 *** 作
2。只有拥有互斥锁的线程才能够 *** 作数据库
3。写 *** 作必须独立拥有互斥锁
4。读 *** 作必须能够共享互斥锁,即在第一次读取的时候获取互斥锁,最后一次读取的时候释放互斥锁
对于数据库来说都是支持并发访问的,但是从 ComboPooledDataSource 中获得的 Connection 连接对象不是线程安全的,一个 Connection 只能在一个线程中,或者局部变量中使用,不得在多个线程中共享一个 Connection。
以上就是关于c# 多线程 *** 作数据库全部的内容,包括:c# 多线程 *** 作数据库、java中如何用多线程访问数据库、java 多线程 *** 作数据库等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)