Connection was forcibly closed error in servlet - java

I am getting an error in my java servlet. The servlet fills the fields of an existing pdf. The page pulls up an alert asking "Do you want to open or save this file?" So if I press "Open" or "Save" it works fine, and does the correct thing. But.... if I press "Cancel", nothing pops up, and I get this error:
com.evermind.server.http.HttpIOException: An existing connection was forcibly closed by the remote host
I am not sure why or where this error is coming about, but it says that the error is happening on this line:
stamp.close();
If more code snippets are needed, please let me know. I just didnt want to paste everything in here, because I dont exactly know where it is happening. Thanks in advance.
EDIT
Here is the majority of my relevant code:
try {
conn = ((DataSource) new InitialContext().lookup(dSource)).getConnection();
stmt = conn.prepareStatement("....");
rs = stmt.executeQuery();
if (rs.next()) {
....
}
stmt = conn.prepareStatement("....");
rs = stmt.executeQuery();
if (rs.next()) {
....
}
if (isTempVerification) {
final String tempFile = "TemporaryVerification.pdf";
try {
response.setHeader(contentDisposition, "attachment; filename=" + tempFile);
reader = new PdfReader(this.getServletContext().getResource("/pdf/" + tempFile));
stamp = new PdfStamper(reader, response.getOutputStream());
form = stamp.getAcroFields();
form.setField("date", current);
form.setField("reply_line", replyLine);
form.setField("first_middle_last", fmlName);
form.setField("term_year_1", termYear + ".");
form.setField("census_date", termCensus);
form.setField("term_year_2", termYear + ".");
//stamp.setFormFlattening(true);
stamp.close();
} catch (IOException e) {
errorFound = true;
e.printStackTrace(System.err);
} catch (DocumentException e) {
errorFound = true;
e.printStackTrace(System.err);
}
} else {
final String officialFile = "OfficialVerification.pdf";
try {
response.setHeader(contentDisposition, "attachment; filename=" + officialFile);
reader = new PdfReader(this.getServletContext().getResource("/pdf/" + officialFile));
stamp = new PdfStamper(reader, response.getOutputStream());
form = stamp.getAcroFields();
form.setField("date", current);
form.setField("reply_line", replyLine);
form.setField("first_middle_last", fmlName);
form.setField("status", studentStatus);
form.setField("hr", hoursTaken);
form.setField("term_year", termYear);
form.setField("start_end_date", termStart + " - " + termEnd);
//stamp.setFormFlattening(true);
stamp.close();
} catch (IOException e) {
errorFound = true;
e.printStackTrace(System.err);
} catch (DocumentException e) {
errorFound = true;
e.printStackTrace(System.err);
}
}
} catch (NamingException e) {
e.printStackTrace(System.err);
} catch (SQLException e){
e.printStackTrace(System.err);
} finally {if (stmt != null) try {stmt.close();
} catch (SQLException e){
e.printStackTrace(System.err);
} if (rs != null) try {rs.close();
} catch (SQLException e){
e.printStackTrace(System.err);}
try {
if (conn != null && !conn.isClosed()) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace(System.err);
}
}

when u make the stamp.close() call, the output stream is also flushed. since you set the response headers before stamp.close(), the response headers are also written to the client. consequently, this causes the file download dialog to appear on the client. when the client clicks 'cancel', the http connection is terminated.
your servlets need to maintain the http connection throughout its execution as it will be writing output to the response output stream. if the http connection is terminated before the response has been committed, you will get the exception that you are seeing now.

Related

FTPClient connection with Java on AWS Virtual Machine

I'm trying to connect to my FTP server in Java SE 1.8. To do so I use this method :
private void connectFTP() {
String server = "ftp.XXXXXXXXXX.site";
int port = 21;
String user = "XXXX";
String pass = "XXXX";
if(!ftpConnexionSuccess.get())
{
client = new FTPClient();
client.configure(new FTPClientConfig(FTPClientConfig.SYST_UNIX));
try {
client.connect(server, port);
ftpConnexionSuccess.set(client.login(user, pass));
if (!ftpConnexionSuccess.get()) {
System.out.println("Could not login to the server");
return;
}
else
{
System.out.println("LOGGED IN SERVER");
client.changeWorkingDirectory("/crypto");
listenedFile = getListenedFile();
System.out.println(listenedFile.getName());
if(listenedFile != null)
{
baseFileTimeStamp.set(listenedFile.getTimestamp().getTimeInMillis());
}
System.out.println(baseFileTimeStamp);
}
} catch (Exception ex) {
System.err.println("FTP connection error : Sleeping for 5 seconds before trying again (" + ex.getMessage() + ")");
ex.printStackTrace();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {e.printStackTrace();}
try {
client.disconnect();
} catch (IOException e) {e.printStackTrace();}
connectFTP();
}
}
}
It works great when I'm on Eclipse and when I export the app on my Windows 10.
Nonetheless, when I try to launch the app on my AWS Webmachine I get a null pointer exception at "listenedFile". The method to listen to this file is the one below.
private FTPFile getListenedFile() {
FTPFile returnedFile = null;
try {
for(FTPFile file : client.listFiles())
{
if(file.getName().contains("filetolisten.txt"))
returnedFile = file;
}
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
try {
client.disconnect();
} catch (IOException e1) {e1.printStackTrace();}
connectFTP();
return getListenedFile();
}
return returnedFile;
}
I thought it was because of the line
client.configure(new FTPClientConfig(FTPClientConfig.SYST_UNIX));
I tried to delete the line, and to replace SYST_UNIX with SYST_NT, but nothing worked.
I tried to delete the line, and to replace SYST_UNIX with SYST_NT, but nothing worked. Also updated Java, updated the common-nets library. Nothing worked

Write Properties to URL

I am trying to the properties from java.util.Properties to a online file using its URL.
This is the code I have so far:
public static boolean savePropertiesToURL(Properties properties, String link, String fileName) {
boolean result = false;
if (properties != null && link != null && fileName != null) {
try {
URL url = new URL(link);
try {
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
try {
OutputStream outStream = connection.getOutputStream();
try {
properties.store(outStream, fileName);
} catch (IOException ex) {
System.err.println("Unable to store the properties: " + ex.getMessage());
} finally {
try {
outStream.flush();
result = true;
} catch (IOException ex) {
Logger.getLogger(PropertiesUtil.class.getName()).log(Level.SEVERE, null, ex);
System.err.println("Unable to flush outputstream: " + ex.getMessage());
}
outStream.close();
}
} catch (IOException ex) {
System.err.println("Unable to get outputstream: " + ex.getMessage());
}
} catch (IOException ex) {
System.err.println("Unable to open URL connexion: " + ex.getMessage());
}
} catch (MalformedURLException ex) {
System.err.println("The URL hasn't been created: " + ex.getMessage());
}
}
return result;
}
It goes thru all that and returns true. Even tho the file is not written.
There are loggers in every catch, but no catch is ever triggered.

Exception handling during database operations

I am bit curious to know in the below code snippet, is there any chances of database connection not being closed. I am getting an issue in the SonarQube telling "Method may fail to close database resource"
try {
con = OracleUtil.getConnection();
pstmtInsert = con.prepareStatement(insertUpdateQuery);
pstmtInsert.setString(++k, categoryCode);
pstmtInsert.clearParameters();
pstmtInsert = con.prepareStatement(updateQuery);
for (i = 0; i < userList.size(); i++) {
pstmtInsert.setString(1, p_setId);
addCount = pstmtInsert.executeUpdate();
if (addCount == 1) {
con.commit();
usercount++;
} else {
con.rollback();
}
}
}
catch (SQLException sqle) {
_log.error(methodName, "SQLException " + sqle.getMessage());
sqle.printStackTrace();
EventHandler.handle();//calling event handler
throw new BTSLBaseException(this, "addInterfaceDetails", "error.general.sql.processing");
}
catch (Exception e) {
_log.error(methodName, " Exception " + e.getMessage());
e.printStackTrace();
EventHandler.handle();//calling event handler
throw new BTSLBaseException(this, "addInterfaceDetails", "error.general.processing");
}
finally {
try {
if (pstmtInsert != null) {
pstmtInsert.close();
}
} catch (Exception e) {
_log.errorTrace(methodName, e);
}
try {
if (con != null) {
con.close();
}
} catch (Exception e) {
_log.errorTrace(methodName, e);
}
if (_log.isDebugEnabled()) {
_log.debug("addRewardDetails", " Exiting addCount " + addCount);
}
}
Thanks in advance
If you are using Java 7+, I suggest you use try-with-resources. It ensures the resources are closed after the operation is completed.
Issue has been resolved when I closed the first prepare statement before starting the another one.
added below code snippet after the line pstmtInsert.clearParameters();
try {
if (pstmtInsert != null) {
pstmtInsert.close();
}
} catch (Exception e) {
_log.errorTrace(methodName, e);
}

Resources not getting released in java

So, I have a function that reads file data, in this case image size. But after it's done it doesn't seem to properly release the files. I can't move those files afterwards. If I don't call this function everything works, but if I do I always get "file in use.. blah blah blah"
private void setMoveType() {
ImageInputStream in = null;
try {
in = ImageIO.createImageInputStream(new FileInputStream(file.toString()));
try {
final Iterator<ImageReader> readers = ImageIO.getImageReaders(in);
if(readers.hasNext()) {
ImageReader reader = readers.next();
try {
reader.setInput(in);
try {
moveType = Helper.getMoveType(new Dimension(reader.getWidth(0), reader.getHeight(0)));
} catch (IOException e) {
System.err.println("IOException: " + e.getMessage());
return;
}
} catch(Exception e) {
System.err.println("ReaderException: " + e.getMessage());
} finally {
reader.dispose();
}
}
} catch(Exception e) {
System.err.println("MoveTypeSetException: " + e.getMessage());
}
} catch (IOException e) {
System.err.print("IOException: failure while creating image input stream");
System.err.println(" -> createImageInputStream Error for file: " + file.getFileName());
return;
} finally {
if(in != null) {
try {
in.close();
} catch (IOException e) {
System.err.println("IOException: " + e.getMessage());
return;
}
}
}
}
EDIT: The ImageInputStream doesn't close properly
EDIT2: a FileInputStream wasn't closed
This stream should also be closed:
new FileInputStream(file.toString())
Closing the stream when you are done should work (in.close()). The operating system prevents the file from being changed, deleted or moved while it is in use. Otherwise, the stream would get messed up. Closing the stream tells the operating system you are no longer using the file.

Avoid automatic jdbc connection close in servlet

I have a servlet deployed on a Jetty 9 server that connects to a MySQL 5.6.17 server using the Connector/J JDBC driver from http://dev.mysql.com/downloads/connector/j/5.0.html.
This particular servlet fires a SQL statement inside a for loop that iterates around 10 times. I have to include the
DriverManager.getConnection(DB_URL, USER, PASSWORD);
line within this loop because the connection closes automatically after the SQL statement has been executed in every iteration of the loop.
Is there a way to keep the connection open, so that getConnection() need be executed only once before the loop starts and then i can manually close it in the finally block.
I have found many posts on this particular issue, but all refer to the connection pooling concept as the solution. But i am just interested in avoiding the connection being closed automatically after each query execution. Shouldn't this be a simple parameter? I am not facing any particular performance problem right now, but it just seems to be a waste of processor and network cycles.
Servlet Code :
public class CheckPhoneNumberRegistrationServlet extends HttpServlet {
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException {
System.err.println("started CheckPhoneNumberRegistrationServlet");
// define database connection details
final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
final String DB_URL = DatabaseParameters.SlappDbURL;
final String USER = DatabaseParameters.DbServer_Username;
final String PASSWORD = DatabaseParameters.DbServer_Password;
PreparedStatement prpd_stmt = null;
Connection conn = null;
ResultSet rs = null;
int resultValue;
// open a connection
/*try {
conn = DriverManager.getConnection(DB_URL, USER, PASSWORD);
} catch (SQLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}*/
JsonParser jsparser;
JsonElement jselement;
JsonArray jsrequestarray;
JsonArray jsresponsearray = new JsonArray();
StringBuffer jb = new StringBuffer();
String line = null;
try {
BufferedReader reader = req.getReader();
while ((line = reader.readLine()) != null)
jb.append(line);
} catch (Exception e) {
e.printStackTrace();
}
try {
jsparser = new JsonParser();
jselement = (JsonElement) jsparser.parse(jb.toString());
jsrequestarray = jselement.getAsJsonArray();
for (int i = 0; i < jsrequestarray.size(); i++) {
// System.err.println("i : " + i +
// jsrequestarray.get(i).toString());
try {
conn = DriverManager.getConnection(DB_URL, USER, PASSWORD);
prpd_stmt = conn
.prepareStatement("select slappdb.isPhoneNumberRegistered(?)");
prpd_stmt.setString(1, jsrequestarray.get(i).toString()
.replace("\"", ""));
rs = prpd_stmt.executeQuery();
if (rs.first()) {
//System.err.println("result sert from sql server : " + rs.getString(1));
//slappdb.isPhoneNumberRegistered() actually returns Boolean
//But converting the result value to int here as there is no appropriate into to Boolean conversion function available.
resultValue = Integer.parseInt(rs.getString(1));
if(resultValue == 1)
jsresponsearray.add(jsparser.parse("Y"));
else if(resultValue == 0)
jsresponsearray.add(jsparser.parse("N"));
else throw new SQLException("The value returned from the MySQL Server for slappdb.isPhoneNumberRegistered(" + jsrequestarray.get(i).toString().replace("\"", "") + ") was unexpected : " + rs.getString(1) + ".\n");
// System.err.println("y");
}
else throw new SQLException("Unexpected empty result set returned from the MySQL Server for slappdb.isPhoneNumberRegistered(" + jsrequestarray.get(i).toString().replace("\"", "") + ").\n");
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (prpd_stmt != null)
prpd_stmt.close();
} catch (SQLException e1) {
}
try {
if (conn != null)
conn.close();
} catch (SQLException e1) {
}
}
}
resp.setStatus(HttpServletResponse.SC_OK);
resp.setContentType("text/plain");
// resp.setContentLength(1024);
resp.getWriter().write(jsresponsearray.toString());
System.err.println(jsresponsearray.toString());
} catch (Exception e) {
// crash and burn
System.err.println(e);
}
The problem is that you're closing the connection inside the for loop. Just move both statements: connection opening and connection close, outside the loop.
conn = DriverManager.getConnection(DB_URL, USER, PASSWORD);
for (int i = 0; i < jsrequestarray.size(); i++) {
try {
//current code...
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (prpd_stmt != null)
prpd_stmt.close();
} catch (SQLException e1) {
}
}
}
try {
if (conn != null)
conn.close();
} catch (SQLException e1) {
}

Categories