Sybase JDBC驱动的改进 2

类别:Java 点击:0 评论:0 推荐:

(续)

Sun J2SE(1.4)的实现对于字符集转换非常缓慢,我用Jfluid做Profile发现一个简单的数据库程序花费在字符集转换上的时间竟然占用了60%以上,因此有必要对转换结果缓存:

 

package com.sybase.jdbc2.jdbc;

 

import java.io.UnsupportedEncodingException;

import java.util.*;

 

public class CachedCharset {

   

    public String convertTo(String src) {

        try {

            String dest = (String)cacheConvertTo.get(src);

            if (dest == null) {

                byte[] bytes = src.getBytes(destCharset);

                dest = new String(bytes, RAW);

                if (cacheConvertTo.size() > EXPIRE_EVERY)

                    cacheConvertTo.clear();

                cacheConvertTo.put(src, dest);

            }

            return dest;

        } catch (UnsupportedEncodingException e) {

            throw new RuntimeException(e.getMessage(), e);

        }

    }

 

    public String castFrom(String dest) {

        try {

            String src = (String)cacheCastFrom.get(dest);

            if (src == null) {

                byte[] bytes = dest.getBytes(RAW);

                src = new String(bytes, destCharset);

                if (cacheCastFrom.size() > EXPIRE_EVERY)

                    cacheCastFrom.clear();

                cacheCastFrom.put(dest, src);

            }

            return src;

        } catch (UnsupportedEncodingException e) {

            throw new RuntimeException(e.getMessage(), e);

        }

    }

   

    public static String convertTo(String src, String charset) {

        if (charset == null) return src;

        if (src == null) return null;

        CachedCharset cc = (CachedCharset)poolCC.get(charset);

        if (cc == null) {

            cc = new CachedCharset(charset);

            poolCC.put(charset, cc); 

        }

        return cc.convertTo(src);

    }

   

    public static String castFrom(String dest, String charset) {

        if (charset == null) return dest;

        if (dest == null) return null;

        CachedCharset cc = (CachedCharset)poolCC.get(charset);

        if (cc == null) {

            cc = new CachedCharset(charset);

            poolCC.put(charset, cc);

        }

        return cc.castFrom(dest);

    }

   

    public CachedCharset(String charset) {

        this.destCharset = charset;

    }

   

    public static final String RAW = "ISO-8859-1";

    String destCharset = null;

   

    public static final int EXPIRE_EVERY = 100000;

    public Map cacheConvertTo = new HashMap();

    public Map cacheCastFrom = new HashMap();

   

    /* Map: destCharset -> CachedCharset */

    public static Map poolCC = new HashMap();

}

 

对于各种Statement,我们给它加上getGeneratedKeys,并给所有的sql加入字符集预处理。Sybase获得自动编号的方法是SELECT @@identity,因为获取自动编号总在插入、更新等操作之后,所以无需担心SELECT操作会破坏上一次的查询结果。(注意,如果自动编号从存储过程产生,而存储过程返回多个ResultSet,且其中某个ResultSet正被程序访问中,那么根据JDBC的一个缺陷,SELECT将会破坏前面的ResultSet。尽管如此,总能返回自动编号。)

对于ResultSet,我们给所有返回的String加上后处理。

对于Connection,它负责解析URL中的新增属性,并建立各种Statement的包装。

 

 

限于CSDN篇幅限制,需要源码的朋友请点击:

Part3èhttp://www.csdn.net/Develop/read_article.asp?id=27125

 

本文地址:http://com.8s8s.com/it/it16578.htm