日期:2014-05-16  浏览次数:20594 次

数据库能力放通(记录)

?? 要求数据库中断的情况下,部分功能能力放通:

?? 实现思路:本想用csv文件存储临时数据,发现用数组拼装太麻烦,后直接采用对象序列化存储

?? 采用读写所操作数据,在业务操作时,捕捉数据库中断异常,把对象写入缓存文件中,

?? 数据库正常后,任意一个用户登陆,开辟一个线程进行读操作,写入数据库,然后删除缓存文件

?

?? 过程中发现学序列化追加存储,无法正常读取,最后一个工具类是处理该问题的

?

?

/**
 * 
 * 缓存文件读取
 * <读取缓存文件,对缓存数据进行数据库持久化操作>
 * 
 * @author  xxx
 * @version  [V01, 2011-9-6]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
@Scope("prototype")
@Component
public class ReadTask extends Thread
{
    private Log log = LogFactory.getLog(ReadTask.class);
    
    /**
     * 文件读写操作类
     */
    @Autowired
    private ReadWriteLockLogic readWriteLockOperator;
    
    @Autowired()
    private EventRepository eventRepository;
    
    /**
     * 执行读取任务
     */
    public synchronized void run()
    {
        
        if (this.readWriteLockOperator != null)
        {
            
            //1、读取文件拼装对象            
            List<EventBean> eventBeanList = null;
            
            try
            {
                eventBeanList = readWriteLockOperator.read();
            }
            catch (Exception e)
            {
                log.error(e.getStackTrace());
            }
            
            //4、删除文件
            readWriteLockOperator.delete();
            
            if (null == eventBeanList || eventBeanList.isEmpty())
            {
                return;
            }
            
            //3、成功保存到数据库(如果出现异常,如果普通异常记录失败行数,如果是数据库中断,回滚)                    
            List<EventBean> faiList = eventRepository.insertEventLink(eventBeanList);
            
            if (null == faiList || faiList.isEmpty())
            {
                return;
            }
            
            //5、对于数据库突然中断的数据,覆写回缓存文件            
            try
            {
                readWriteLockOperator.write(faiList, false);
            }
            catch (Exception e)
            {
                log.error(e.getStackTrace());
            }
        }
    }
    
}
/**
?* 文件读写操作类
?* <针对文件的续写加入读写锁,避免冲突>
?* 
?* @author? xxx
?* @version? [V1.0, 2011-9-6]
?* @see? [相关类/方法]
?* @since? [ECC/C02]
?*/
@Component
public class ReadWriteLockLogic
{
??? 
??? /**
???? * 文件路径
???? */
??? private String path;
??? 
??? /**
???? * 初始化一个 ReadWriteLock
???? */
??? private ReadWriteLock lock = new ReentrantReadWriteLock();
??? 
??? private Log log = LogFactory.getLog(ReadWriteLockLogic.class);
??? 
??? /**
???? * 读数据缓存文件
???? * 
???? * @return EventBean
???? */
??? public List<EventBean> read()
??? //??????? throws Exception
??? {
??????? 
??????? // 得到 readLock 并锁定
??????? Lock readLock = lock.readLock();
??????? readLock.lock();
??????? 
??????? List<EventBean> eventBeanList = new ArrayList<EventBean>();
??????? 
??????? ObjectInputStream ois = null;
??????? 
??????? FileInputStream fis = null;
??????? 
??????? File file = new File(path);
??????? 
??????? //赋予创建文件权限
??????? //??????? file.setWritable(true, false);
??????? 
??????? if (!file.exists())
??????? {
??????????? return null;
??????? }
??????? try
??????? {
??????????? fis = new FileInputStream(file);
??????????? 
??????????? ois = new ObjectInputStream(fis);
??????????? 
??????????? Object obj;
??????????? 
??????????? while (fis.available() > 0)
??????????? {
??????????????? obj = ois.readObject();
??????????????? if (obj instanceof EventBean)
??????????????? {
??????????????????? EventBean eventBean = (EventBean)obj;
??????????????????? eventBeanList.add(eventBean);
??????????????? }
??????????? }
??????? }
??????? catch (Exception e)
??????? {
??????????? log.error(e.getStackTrace());
??????? }
??????? finally
??????? {
??????????? if (null != fis)
??????????? {
??????????????? try
??????????????? {
??????????????????? fis.close();
??????????????? }
??????????????? catch (IOException e)
??????????????? {
??????????????????? log.error(e.getStackTrace());
??????????????? }
???????????