473,396 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Using Keep-Alive to retain connection for duration

116 64KB
Hi.

I am building an app that acquires data from a web service via HttpURLConnection. The app requests data every minute continuously whilst it is open and activated.

At the moment I am opening and closing the connection each time it is required but is it better practise to have a single connection that persists?

The current code is

Expand|Select|Wrap|Line Numbers
  1. URL url = new URL(tradeURL);
  2. connection = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);
  3. connection.setRequestProperty("Content-Type", "application/json");
  4. connection.setRequestProperty("Content-Length", Integer.toString(urlParameters.getBytes().length));
  5. connection.setUseCaches(false);
  6. connection.setDoOutput(true);
  7. DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
  8. wr.writeBytes(urlParameters);
  9. wr.close();
  10. InputStream is = connection.getInputStream();
  11. BufferedReader rd = new BufferedReader(new InputStreamReader(is));
  12. StringBuilder response = new StringBuilder();
  13. String line;
  14. while((line = rd.readLine()) != null) {
  15.     response.append(line);
  16.     response.append('\r');
  17. }
  18. rd.close();
  19. connection.disconnect();
  20.  
If it is better to re-use the connection from the initial request, how would I go about doing that?

Also, is the keep-alive related to the domain level only - ie api.abcd.com - or is the entire url path involved?

Many thanks!
Sep 22 '15 #1

✓ answered by chaarmann

Use a connection pool!

Your problem is not only that a connection is kept alive even if it is not used anymore, but also that the maximum number of connections is limited.
This is all managed by a connection pool. (I have written my own connection pool many years ago, but now there are a lot of ready-made connection pools out for free usage. Just google for that.)
Instead of opening and closing connections every time which is very costly, a connection pool will re-use existing connections.

3 1599
chaarmann
785 Expert 512MB
Use a connection pool!

Your problem is not only that a connection is kept alive even if it is not used anymore, but also that the maximum number of connections is limited.
This is all managed by a connection pool. (I have written my own connection pool many years ago, but now there are a lot of ready-made connection pools out for free usage. Just google for that.)
Instead of opening and closing connections every time which is very costly, a connection pool will re-use existing connections.
Sep 23 '15 #2
robertybob
116 64KB
Many thanks Chaarmann - I'm investigating this and will let you know.
Sep 24 '15 #3
robertybob
116 64KB
Sorry - had to work on something else for a while and forgot all about this!

For my needs I managed to get good enough stability using the generic keep-alive functionality and tweaks to the requests but Chaarmann's method seems to be decent solution for more complex requirements.

Many thanks.
Oct 14 '15 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: meng | last post by:
is it possible to use a single connection object shared by several tasks where each task is handled by a thread? these tasks call stored procedures that return record sets, no editing, update or...
3
by: Ralf Assmann | last post by:
Hi there, is it correct that, using one db2-connection with UR, the connection will "switch" to CS after making an insert or update on a table within this connection? That means, reading a...
8
by: ZRexRider | last post by:
Hi, I have an application that uses MS-SQL Linked tables. I have a utility routine that drops all links and re-establishes them. It works great when my connection string is a DSN connection...
1
by: timandsuzi36 | last post by:
Here's the issue. Appreciate any help any Access gurus can offer. I have a .NET WinForms application that needs to launch a Access DB application used for generating Access Reports. That part...
1
by: yma | last post by:
Hi, I tried to use a listbox to display a column in MS Access 2000 nwind.mdb by using ole DataAdapter, Connection and dataset controls. But I got "It is already opened exclusively by another...
12
by: Charlie | last post by:
Hi: My host will not allow me use a trusted connection or make registry setting, so I'm stuck trying find a way to hide connection string which will be stored in web.config file. If I encrypt...
2
by: news.microsoft.com | last post by:
Hi, (Vb.net 2003 and Crystal report.net) If my application already has a connection, how can I use it in the crystal report. I have designed some reports using the designer with vs.net03,...
1
by: funcSter | last post by:
I want to retrieve data from an Excel file like how I would with a database. I understand that I would have to use OLE DB. Somehow I think I cannot get the connection string right, as the bit of...
3
sovitagar
by: sovitagar | last post by:
I know how to transfer files over a network but I have a confusion whether or not can we send a Zipped file from server to client using a TCP connection. P.S. : the code is to be written in C#...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.