Fork me on GitHub

Eclipse初次java开发问题总结-2

今天对之前写的servlet程序做了个简单的性能测试发现了一些问题,经过解决这些问题没有再重现,有些问题自己确切知道原因,有的则不太确定。

1、配置文件读取问题

项目中使用.properties作为配置文件,刚开始读取方法如下,使用的是Properties

public  class ConfigHelper {
    
    private static Properties properties = new Properties();
    /***
     * 
     * @param propertyName:属性的key
     * @param defaultVal:默认值
     * @return 返回配置文件名值,若找不到配置的key,返回默认值
     * @throws IOException
     */
    public static String getPropertyByKey(String propertyName, String defaultVal) throws IOException {
        String fileName = ("/config.properties"); 
        InputStream  fin = null;
        String value = null;
        try {
            fin = ConfigHelper.class.getResourceAsStream(fileName);
            properties.load(fin);
            value =  properties.get(propertyName).toString();
          
        } catch (FileNotFoundException e) {
            return defaultVal;
        } catch (IOException e) {
            return defaultVal;
        }
        finally
        {
             fin.close();
        }
        
        return value;
    }
}

 在没有压力的情况下这种方法是没问题的,但一旦有点并发发现这样使用Properties是有问题的,下图压力测试下出现的问题:

 

刚开始尝试使用synchronized

public static synchronized String getPropertyByKey(String propertyName, String defaultVal)

 当时问题并没有解决,此处省去N个字,最后解决方法如下,声明一个类SafeProperties继承自Properties,该类为单例:

/**
* 读取Properties属性文件
* @author jdzhan
*/
public class SafeProperties extends Properties {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    private static SafeProperties instance;

    public static SafeProperties getInstance(String path){
        if (instance != null){
            return instance;
        }else {
            makeInstance(path);
            return instance;
        }
    }

    private static synchronized void makeInstance(String path){
        if (instance == null){
            instance = new SafeProperties(path);
        }
    }

    private SafeProperties(String path){
        InputStream is = getClass().getResourceAsStream(path);
        try {
            load(is);
        } catch (IOException ex) {
            System.err.println("错误信息:  读取属性文件失败!");
            System.err.println("请确认 【"+ path +"】 文件是否存在。");
        }
    }

}

 然后在修改ConfigHelper如下:

public class ConfigHelper {

    private static Properties properties = null;
    
    static{
        String fileName = ("/config.properties");
        properties=SafeProperties.getInstance(fileName);
    }

    /***
     * 
     * @param propertyName
     *            :属性的key
     * @param defaultVal
     *            :默认值
     * @return 返回配置文件名值,若找不到配置的key,返回默认值
     * @throws IOException
     */
    public static synchronized String getPropertyByKey(String propertyName, String defaultVal)
            throws IOException {
        InputStream fin = null;
        String value = null;
        try {
            value = properties.get(propertyName).toString();
        } catch (Exception e) {
            LogHelper.writeLog("读取配置文件出错:"+ExceptionUtils.getFullStackTrace(e));
            return defaultVal;
        } finally {
            if (fin != null) {
                fin.close();
            }
        }

        return value;
    }
}

 

2、Mysql:Communications link failure

在压力测试下Mysql也报了一大堆问题,其中有一个问题如下:

这个问题我找到一个比较有参考意义的连接:

http://stackoverflow.com/questions/2121829/mysql-jdbc-communications-link-failure

但是我最后解决这个问题(可能根本没解决)方法是将Mysql的max_connections从1000增加到10000。

 

3、Mysql:No operations allowed after statement closed.

这个问题折腾我时间最长,结果发现原来是自己代码的问题,因为静态变量的原因,细节不说了。最后得出结论是:这个问题“顾名思义”,一般是因为你后面使用了前面已经关闭的数据库连接造成的。

 

posted @ 2013-06-19 19:39  zhanjindong  阅读(471)  评论(0编辑  收藏  举报
TOP