Analysis and Solution of Typical error applications in a Java Singleton Mode

Source: Internet
Author: User

Questions from the Forum,

Http://topic.csdn.net/u/20090927/23/497372f4-af98-4b7b-8489-3eb3a8de43b5.html

 

ItsCodeAs follows:

Import Java. SQL. connection; <br/> Import Java. SQL. preparedstatement; <br/> Import Java. SQL. resultset; <br/> Import Java. SQL. sqlexception; <br/> Import COM. CFD. DRP. utils. utils; <br/> public class clientmanager {<br/> // instance-level attributes are used here <br/> stringbuffer sb = new stringbuffer (); <br/> // The Singleton mode is implemented here <br/> Private Static clientmanager manager = new clientmanager (); <br/> private clientmanager () {<br/>}< br/> Public static clientmanager getinstance () {<br/> return manager; <br/>}< br/> // No synchronization is performed here, multiple Threads call this method simultaneously <br/> Public String showtree () {<br/> connection = NULL; <br/> Sb. delete (0, sb. length (); <br/> try {<br/> connection = utils. getconnection (); <br/> tree (connection, 0, 1); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}finally {<br/> utils. close (connection); <br />}< Br/> return sb. tostring (); <br/>}< br/> Public void tree (connection, int ID, int level) throws sqlexception {<br/> string SQL = "select * From t_client where pid =? "; <Br/> int I = 0; <br/> preparedstatement PS = NULL; <br/> resultset rs = NULL; <br/> try {<br/> PS = connection. preparestatement (SQL); <br/> ps. setint (1, ID); <br/> rs = ps.exe cutequery (); <br/> while (RS. next () {<br/> Sb. append (RS. getstring ("name") + "<br>" + "/N"); <br/> If (RS. getstring ("is_leaf "). equals ("N") {<br/> tree (connection, Rs. getint ("ID"), Level); <br/>}< br/>} finally {<br/> utils. slose (RS); <br/> utils. close (PS); <br/>}< br/>

 

I have marked the specific error in the code.

Singleton means that the system has only one instance, So multiple threads use this instance, and of course this stringbuffer is used at the same time.

How can we avoid chaos by dumping garbage in it together!

 

The solution is to make stringbuffer a local variable.

Import Java. SQL. connection; <br/> Import Java. SQL. preparedstatement; <br/> Import Java. SQL. resultset; <br/> Import Java. SQL. sqlexception; <br/> Import COM. CFD. DRP. utils. utils; <br/> public class clientmanager {<br/> // The Singleton mode is implemented here <br/> Private Static clientmanager manager = new clientmanager (); <br/> private clientmanager () {<br/>}< br/> Public static clientmanager getinstance () {<br/> return Manager; <Br/>}< br/> // No synchronization is performed. Multiple Threads call this method at the same time. <br/> Public String showtree () {<br/> // use the local variable <br/> stringbuffer sb = new stringbuffer (); <br/> connection = NULL; <br/> try {<br/> connection = utils. getconnection (); <br/> tree (connection, 0, 1, Sb); // call as a parameter <br/>} catch (exception E) {<br/> E. printstacktrace (); <br/>}finally {<br/> utils. close (connection); <br/>}< br/> return sb. t Ostring (); <br/>}< br/> Public void tree (connection, int ID, int level, stringbuffer SB) throws sqlexception {<br/> string SQL = "select * From t_client where pid =? "; <Br/> int I = 0; <br/> preparedstatement PS = NULL; <br/> resultset rs = NULL; <br/> try {<br/> PS = connection. preparestatement (SQL); <br/> ps. setint (1, ID); <br/> rs = ps.exe cutequery (); <br/> while (RS. next () {<br/> Sb. append (RS. getstring ("name") + "<br>" + "/N"); <br/> If (RS. getstring ("is_leaf "). equals ("N") {<br/> tree (connection, Rs. getint ("ID"), level, Sb); <br/>}< br/>} finally {<br/> utils. slose (RS); <br/> utils. close (PS); <br/>}< br/>

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.