Summary of Eclipse's first Java Development-2

Source: Internet
Author: User

Today, I did a simple performance test on the servlet program I wrote and found some problems. After solving these problems, I did not reproduce them. I knew the cause for some problems, but I was not sure about them.

1. configuration file reading problems

Used in the project.PropertiesAs the configuration file, the read method is as follows:Properties:

Public class confighelper {Private Static Properties properties = new properties ();/***** @ Param propertyname: attribute key * @ Param defaultval: the default value * @ return indicates the configuration file name value. If the configuration key cannot be found, the default value * @ throws ioexception */public static string getpropertybykey (string propertyname, string defaultval) is returned) 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 ;}}

This method is okay if there is no pressure, but this method is used once a bit of concurrency is foundPropertiesThere is a problem with the stress test:

 

First try to useSynchronized:

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

At that time, the problem was not solved. N words were omitted here. The final solution is as follows: declare a classSafepropertiesInherited fromProperties, Which is a singleton:

/*** Read the properties file * @ 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 ("error message: failed to read attribute file! "); System. Err. println (" check whether the ["+ path +"] file exists. ");}}}

Then modifyConfighelperAs follows:

Public class confighelper {Private Static Properties properties = NULL; static {string filename = ("/config. properties "); properties = safeproperties. getinstance (filename);}/***** @ Param propertyname *: attribute key * @ Param defaultval *: Default Value * @ return indicates the configuration file name value, if the configured key is not found, the default value * @ throws ioexception */public static synchronized string getpropertybykey (string propertyname, string defaultval) throws Io is returned. Exception {inputstream fin = NULL; string value = NULL; try {value = properties. get (propertyname ). tostring ();} catch (exception e) {loghelper. writelog ("An error occurred while reading the configuration file:" + exceptionutils. getfullstacktrace (e); Return defaultval;} finally {If (Fin! = NULL) {fin. Close () ;}} return value ;}}

 

2. MySQL: communications link failure

In the stress test, MySQL also reported a lot of problems, one of which is as follows:

In this case, I found a reference connection:

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

However, the final solution to this problem (which may not be solved at all) isMax_connectionsIncrease from 1000 to 10000.

 

3. MySQL: No operations allowed after statement closed.

This problem took me the longest time, and I found that it was my own code problem, because of static variables, the details are not mentioned. The final conclusion is that the question "as the name suggests" is generally caused by using the previously closed database connection.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.