📄 dbimpl2.java
字号:
package org.com.gather;
import java.util.*;
import java.sql.*;
import org.com.gather.exception.*;
//切换了类,原版本事DBImpl,考虑到功能上的限制
//开发了另一个版本,DBImpl2,直接利用框架来切换类
public class DBImpl2 implements DB{
private Properties pro = null;
private Log log = null;
private BackUp back = null;
private static String key = "key";
private static DBImpl2 instance = null;
//属性由Spring注入
private DBImpl2(Properties pro){
this.pro = pro;
}
//私有的构造器,为了获取实例,要提供一个静态方法,
//通过该方法获取实例
public static DBImpl2 getInstance(Properties pro){
if(instance == null){
synchronized(key){
if(instance == null){
instance = new DBImpl2(pro);
}
}
}
return instance;
}
//有可能多个线程会同时保存数据,也就是说这个方法
//将会出现并发问题,为了防止多个线程同时对做入库
//操作,在这加一个同步锁.
public synchronized void storeToDB(Collection col)throws SQLException,ClassNotFoundException,DBException{
log.writeInfo("应该入库数量大下---"+col.size());
if(!col.isEmpty()){
long start = System.currentTimeMillis();
Connection con = null;
con = this.getConnection();
log.writeDebug("成功获取连接,程序继续执行!");
long end = System.currentTimeMillis();
long time = (end - start)/1000;
log.writeInfo("获取连接时长为 --->"+time);
//构建一块缓存
Collection coll = new ArrayList();
//取出batchSize,每次入库量大小
String s_size = pro.getProperty("batchSize");
int size = new Integer(s_size).intValue();
int c_size = col.size();
//对入库集合进行遍历
Iterator iter = col.iterator();
//为了测试,这里只插入一个字段
String sql = "insert into dt values(?)";
PreparedStatement pstmt = null;
try{
pstmt = con.prepareStatement(sql);
}catch(SQLException e){
e.printStackTrace();
log.writeError("解析sql语句出异常!");
back.storeForEmpty(col);
log.writeInfo("成功把入库数据备份!");
System.exit(0);
}
while(iter.hasNext()){
//把集合的数据存放到缓存,等到数量到了
//batch-size指定的数量后再一次入库;
coll.add(iter.next());
//如果集合数据存放到缓存,则可把集合的数据
//删除;
iter.remove();
if(coll.size() == size){
try{
con.setAutoCommit(false);
Iterator it = coll.iterator();
while(it.hasNext()){
BIDR bidr = (BIDR)it.next();
pstmt.setString(1,bidr.getLoginName());
pstmt.addBatch();
}
pstmt.executeBatch();
con.commit();
}catch(SQLException e){
log.writeError(e.getMessage());
//把剩下没入库的数据,一并保存;
col.addAll(coll);
back.storeForEmpty(col);
log.writeInfo("入库过程重出异常,成功把数据备份到文件;");
try{
con.rollback();
log.writeInfo("入库异常,回滚!");
}catch(SQLException ee){
ee.printStackTrace();
log.writeError("数据库异常,数据回滚!");
throw new DBException(ee);
}
throw new DBException(e);
}
//把缓存清空,重新载入数据;
coll.clear();
}
}
//把剩余的数据,继续入库;
Iterator itr = coll.iterator();
try{
con.setAutoCommit(false);
while(itr.hasNext()){
BIDR br = (BIDR)itr.next();
pstmt.setString(1,br.getLoginName());
pstmt.addBatch();
}
pstmt.executeBatch();
con.commit();
}catch(SQLException e){
e.printStackTrace();
back.storeForEmpty(coll);
log.writeInfo("成功把剩余数据备份!");
try{
con.rollback();
}catch(SQLException ee){
ee.printStackTrace();
log.writeError("入库异常,剩余数据回滚!");
throw new DBException(e);
}
}
//清空缓存;
coll.clear();
//把剩余的集合数据清空;
col.clear();
log.writeDebug("成功将数据插入数据库!");
}else{
System.out.println("入库数据量为零!");
}
}
//这里每次都是拿到物理连接,可以考虑用连接池.
public Connection getConnection()throws SQLException,ClassNotFoundException{
Class.forName(pro.getProperty("driver"));
Connection con = DriverManager.getConnection(pro.getProperty("url"),pro.getProperty("username"),pro.getProperty("password"));
return con;
}
public void setLog(Log log){
this.log = log;
}
public void setBackUpServer(BackUp back){
this.back = back;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -