jsp - TNS error on Tomcat -
I am working on a social networking website project. The user who will be displayed to shorten their code from that website page, I have created a separate class which I have included in my various pages and I use different functions defined in class.
Example: I want to display user details, this is the code for it:
User objUser = new user (); Results set rs = objUser.getAllDetails (userName); Rs.next (); Data = "& lt; div id = text & gt;" + "From the & lt; Table & gt;" + "From & lt; tr & gt; & lt; td colspan = 2 & gt; & lt; h1 & gt; User Details & lt; bell & gt; & lt; / h1 & gt; & lt; / td & Lt; / T & gt; & lt; TR & gt; & lt; TD & gt; First Name: & lt; / TD & gt; & lt; TD & gt; "+ rs .getString ("firstname") + '& lt; / TD & gt; & Lt; / TR & gt; From "+" to & lt; TR & gt; & Lt; TD & gt; Last name: & lt; / TD & gt; & Lt; TD & gt; "+ Rs.getString (" aliases ") + '& lt; / td> gt; & lt; / tr & gt; + "From & lt; TR & gt; & lt; TD & gt; Username: & lt; / TD & gt; & lt; TD & gt;" + rs.getString ("username") + '& lt ; / TD & gt; & Lt; / TR> from "+" to & lt; TR & gt; & Lt; TD & gt; Mail ID: & lt; / TD & gt; & Lt; TD & gt; "+ Rs.getString (" mailid ") + '& lt; / td> gt; & lt; / tr & gt; + "From & lt; TR & gt; & lt; TD & gt; Contacts: & lt; /td><td>"+rs.getString("contact")+"</td></tr> ; "+" & Lt; Tr & gt; & lt; TD & gt; Course: + +getgetstring ("course") + "& lt; / Td> & Lt; / Tr & gt; "+" & lt; Tr & gt; & Lt; TD & gt; Branch: & lt; / Td> & Lt; Td> "+ rsgetstring (+" branch ") +" & lt; / Td> & Lt; / Tr & gt; "+" & lt; Tr & gt; & Lt; TD & gt; Year: & lt; / Td> & Lt; Td> "+ rsgetString (" year ") +" & lt; / Td> & Lt; / Tr & gt; "+" & lt; / Table> "+" & lt; / Div> This is my code to get the details. My class name is user, I make an object of it and use the function defined in it As a result, I get the result which I process as a string and print it later.
This is my code, Functon getAllDetails ()
Get public results all the details (string user no ) {Result as rs = null; try {class.forName (application.getInitParameter ("driver"); connection con = DriverManager.getConnection (application.getInitParameter ("url"), application.getInitParameter ("user name"), application .getInitParameter ("password"); Details: st = con.createStatement (); Rs = st.executeQuery ("select from userfine * username = '" + username "+' ''); Returns RS; } Hold (exception e) {return RS; }} Now, the problem is that if I run my webpad for some time, I get a TNS error, which I believe is because there is no open connection Because all the connections have been captured. The problem is that I am not closing the connection, which I install in my getDetails () function, but if I try to close the connection before returning the ResultSet, it shows an error that the connection is closed has gone. If I turn it off after returning the object, I do not think that it does not make any sense, but I also tried to do it and the errors say that the code is never executed after the return statement. And if I try to close the connection in the block at the end, then it does not recognize the variable and the conif.
I believe that the number of connections will increase, but I do not think it is good, because if ever I live this app and there are many users for it, then I can increase the number of connections not can.
Please help, thank you already for your help. I hope I want to ask myself what I want.
You are doing it wrong First of all the connection () is in a wrong place, very deep is. Use a similar () in the beginning of a user session, at the same level of code where you can finally claim that all the results are read from the database and you can safely close the connection ()
Or better, Google "connection pooling" to see how you can design it more efficiently.
Comments
Post a Comment