Eclipse初次java开发问题总结-2
2024-10-13 12:23:19
今天对之前写的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.
这个问题折腾我时间最长,结果发现原来是自己代码的问题,因为静态变量的原因,细节不说了。最后得出结论是:这个问题“顾名思义”,一般是因为你后面使用了前面已经关闭的数据库连接造成的。
最新文章
- Eclipse调试Android App若选择“Use same device for future launches”就再也无法选择其他设备的问题
- git使用详细介绍
- spring java 获取webapp下文件路径
- 分层导航and隐藏导航
- ios openURL的使用(调用系统电话、浏览器、地图、邮件等)
- 嵌入式linux内核是什么?
- UVa 10256 (判断两个凸包相离) The Great Divide
- AngularJS移动开发中的坑汇总
- 字典:当索引不好用时 - 零基础入门学习Python025
- SQL基础查询实战
- centos 6.* 配置端口
- Clojure新手入门
- 【凡尘】---react-redux---【react】
- 求数组中两数之和等于target的两个数的下标
- Connection failed Flowsocketconnector Failed to connect to target addressWindows error10061:由于目标计算机积极拒绝,无法连接
- angular笔记_8(事件)
- Python学习笔记(day23更新)
- A Statistical Model for Scientific Readability-paper
- Some questions after Reading 《移山之道》
- 推送消息 web push notification
热门文章
- 【小白的CFD之旅】18 控制方程基础
- DOUHAO
- analysis-what-blockchain-technology-means-for-artificial-intelligence-cm888540
- django rest_framework入门五-认证和权限
- setRequestedOrientation
- 14款超时尚的HTML5时钟动画
- 【自动化测试】selenium之 chromedriver与chrome版本映射表
- Oracle 重启数据库实例
- [转]最全的用正则批量去除Teleport Pro整站下载文件冗余代码
- 【WPF】使用控件MediaElement播放视频