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/>