Retrofit calls to jersey Rest API issue <p>i am banging my head over this issue but i can not tell what it is exactly.. Anyway i have a mobile application that uses Retrofit 2 to make calls to my jersey Rest server, i will post the code below and try to explain what happens.</p> <p>Server side Database connector</p> <pre><code>public class Connector { private Context initCtx = null; private Context envCtx; private DataSource ds; public Connection con; public Connection getDbConnection () throws SQLException{ try { Class.forName("org.mariadb.jdbc.Driver").newInstance(); initCtx = new InitialContext(); envCtx = (Context) initCtx.lookup("java:comp/env"); ds = (DataSource) envCtx.lookup("jdbc/kokosinjac"); con = ds.getConnection(); System.out.print("Got Connection"); }catch (Exception ex){ // con.close(); System.out.print("Could not establish connection to the Database"); ex.printStackTrace(); } return con;} } </code></pre> <p>Comment service</p> <pre><code>@Path ("/commentService") public class CommentService { CommentDAO cmDao = new CommentDAO(); @POST @Path ("/insertRecords") @Consumes (MediaType.APPLICATION_JSON) public void insertData (Comment comment) throws SQLException{ System.out.println("Do we have something here? : "+ comment.getUploader()); cmDao.insertTextData(comment); } @GET @Path ("/listComments") @Produces (MediaType.APPLICATION_JSON) public List getComments (@QueryParam ("catId")int catId,@QueryParam ("itemId")int itemId) throws SQLException { return cmDao.getComments(catId,itemId);} } </code></pre> <p>Comment DAO</p> <pre><code>public class CommentDAO { public void insertTextData (Comment comment) throws SQLException{ Connector dbConnector = new Connector(); Connection con = null; PreparedStatement preparedStmt; String query = "INSERT into comments (catId,itemId,comment,uploader,date)" + "VALUES (?,?,?,?,?)"; Date todaysDate = new Date(); DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { con = dbConnector.getDbConnection(); preparedStmt = con.prepareStatement(query); preparedStmt.setInt(1,comment.getCatId() ); preparedStmt.setInt(2,comment.getItemId()); preparedStmt.setString(3, comment.getComment()); preparedStmt.setString(4, comment.getUploader()); preparedStmt.setString(5, df.format(todaysDate)); preparedStmt.execute(); con.close(); dbConnector.con.close(); preparedStmt.close(); con = null; dbConnector = null; preparedStmt = null; }catch (Exception ex){ System.out.println("Insert comment exception"); ex.printStackTrace(); }finally{ if (con != null){ try { con.close(); }catch (Exception ex){ ex.printStackTrace(); } con = null; } if (dbConnector != null){ try { dbConnector.con.close(); }catch(Exception ex){ex.printStackTrace();} dbConnector = null; } } } public List&lt;Comment&gt; getComments (int catId,int itemId) throws SQLException { List&lt;Comment&gt; commentData = new ArrayList&lt;&gt;(); Comment comment = null; Connector dbConnector = new Connector(); ResultSet rs = null; Statement stmt = null; Context initCtx= null; Context envCtx = null; DataSource ds =null; Connection con = null; try { con = dbConnector.getDbConnection(); stmt = con.createStatement(); rs = stmt.executeQuery("SELECT * FROM comments WHERE catId = "+catId+" AND itemId ="+itemId+" ORDER BY date DESC"); while (rs.next()){ comment = new Comment(); comment.setComment(rs.getString("comment")); comment.setUploader(rs.getString("uploader")); comment.setDate(rs.getString("date")); commentData.add(comment); } con.close(); dbConnector.con.close(); rs.close(); stmt.close(); con = null; dbConnector = null; rs = null; stmt = null; }catch (Exception ex){ System.out.println("Get comment list exception"); ex.printStackTrace(); }finally { if (con != null){ try { con.close(); }catch (Exception ex){ ex.printStackTrace(); } con = null; } if (dbConnector != null){ try { dbConnector.con.close(); }catch(Exception ex){ex.printStackTrace();} dbConnector = null; } if (rs != null){ try { rs.close(); }catch (Exception ex){ ex.printStackTrace(); } rs = null; } if (stmt !=null){ try { stmt.close();}catch (Exception ex){ ex.printStackTrace(); } stmt = null; } } return commentData; } } </code></pre> <p>Android app Activity</p> <pre><code>........ void commentClick (View v){ ..... listComments(categoryId,item,context); ...... } public void listComments (int catId,int itemId, final Context cont){ RetrofitAPInterface apiService = ApiClient.getClient().create(RetrofitAPInterface.class); Call&lt;ArrayList&lt;Comment&gt;&gt; call = apiService.getComments(catId, itemId); call.enqueue(new Callback&lt;ArrayList&lt;Comment&gt;&gt;() { @Override public void onResponse(Call&lt;ArrayList&lt;Comment&gt;&gt; call, Response&lt;ArrayList&lt;Comment&gt;&gt; response) { ArrayList&lt;Comment&gt; commArr = response.body(); Log.i(TAG, "onResponse: ARRAY"+commArr); combinedArr = new ArrayList&lt;Comment&gt;(); updateCommentArray(commArr); populateListView(cont); } @Override public void onFailure(Call&lt;ArrayList&lt;Comment&gt;&gt; call, Throwable t) { } }); } private ArrayList&lt;Comment&gt; updateCommentArray (ArrayList&lt;Comment&gt; commArray){ for (Comment o : commArray) { combinedArr.add(o); } return combinedArr;} private void populateListView ( final Context cont){ ListView lView = (ListView) findViewById(R.id.commentsListView); if (!uploadClicked){ listAdapter = new AdapterComment(cont, R.layout.custom_row_comment,combinedArr ); lView.setAdapter(listAdapter); } else{ uploadClicked = false; //lView.setAdapter(listAdapter); listAdapter.notifyDataSetChanged(); } } void uploadComment (View v){ ..... final Comment com = new Comment(catId, itemIdint, comment, uploader, date); RetrofitAPInterface apiService = ApiClient.getClient().create(RetrofitAPInterface.class); Call&lt;Comment&gt; call = apiService.insertComment(com); Log.i(TAG, "uploadComment: pre enqueue"); call.enqueue(new Callback&lt;Comment&gt;() { @Override public void onResponse(Call&lt;Comment&gt; call, Response&lt;Comment&gt; response) { Comment check = response.body(); Context context = getApplicationContext(); int duration = Toast.LENGTH_SHORT; //IF COMMENT PASSED if (response.code() == 204) { InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); uploadPb.setVisibility(View.GONE); CharSequence text = "Komentar poslat"; Toast toast = Toast.makeText(context, text, duration); toast.show(); commentText.setText(""); uploaderText.setText(""); combinedArr.add(0,com); populateListView(context); //IF IT DID NOT PASS } else { uploadPb.setVisibility(View.GONE); CharSequence text = "Komentar nije poslat"; Toast toast = Toast.makeText(context, text, duration); toast.show(); } call.cancel(); Log.i(TAG, "onResponse: Response" + response.code()); } @Override public void onFailure(Call&lt;Comment&gt; call, Throwable t) { Context context = getApplicationContext(); uploadPb.setVisibility(View.GONE); AlertDialog.Builder alert = new AlertDialog.Builder(context); alert.setTitle("Server"); alert.setMessage("Ups, nesto nije uredu sa serverom, komentar nije poslat"); alert.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { finish(); } }); alert.show(); } }); ..... } </code></pre> <p>Ok, so in that activity i have a FAB button that switches between views, when it switches to comment view it requests all comments for that picture, and in comment view i can upload a new comment. Everything works fine except this: for example if i spam the FAB button lets say 6 times and i end up on comment view and upload a new comment, on my tomcat server log i will get</p> <pre><code>EG. Got Connection Got Connection Got Connection Got Connection Got Connection Got Connection Do we have something here? uploadedComment </code></pre> <p>The dilemma here is : Is it a memory leak, and if it is how do i make a brand new call every time. Or does the System.out.print write everything in the end, and if it is why doesn't it write Got connection everytime i swipe the screen and it downloads the new comments. If someone could clarify this i would be really really greatfull! Thanks :D</p>