0

I have the scenario where I need to stream from DB the content of the file directly to the client browser.

So I use plain jdbc rs.getBlob() then blob.getBinaryStream(); later writing into an http outputstream.

What I noticed (a very good thing ) is that once I have the inputstream via the blob, the db connection is retuned to the the datasource pool. (weblogic)

Now i ask you , my observation is correct ? cause I had the fear that for downloads that will take long time the db connection will stay to the request in order to be able to stream the file.

Apparently once streaming started the DB connection is not used anymore.

will mockup here some code for better understanding

@Trasactional
public void InputStream getIsFromBlob(....){

 ....
is = blob.getBinaryStream();
...

return is;

}

Later this method is used in a servlet let's say and write the contect of is in the http outputstream

Thanks

Cris
  • 4,947
  • 6
  • 44
  • 73

2 Answers2

0

Without a peek at the code, I dont think what you think is correct. You have to specifically close the connection for it to close (typically in the finally block), or specify how long a connection can be active on the database. From here : It's the DB server who will timeout and reclaim it. Refer the DB server specific administration manual for defaults and how to change it. In case of for example MySQL, it's the wait_timeout setting which defaults to 28800 seconds (8 hours).

Community
  • 1
  • 1
happybuddha
  • 1,271
  • 2
  • 20
  • 40
  • The connection is closed automatically by the container (jee app) when it exists the method and passed as result the inputstream – Cris Mar 12 '13 at 13:29
0

What you said is true that connection can be closed automatically by the container. When all the references to a connection are gone, the connection is returned to the connection pool during garbage collection. However, this is usually considered bad programming practice since it is usually the main cause of connection leak. I would recommend closing the connection whenever you do not need it through finally block.

To your original question, I would say your observation is correct. First, you read content from the blob though the binary inputstream. This streaming requires DB connection. But after the read from inputstream is done, you do not need the DB connection any more. In your case, the method exists and garbage collection kicks in and return the connection back to the pool. Later your write the binary content streaming to your browser through HttpOutputStream. This streaming is not related to DB.

Again, I would strongly recommend not rely on the garbage collection to return connections to the connection pool.

Lan
  • 6,470
  • 3
  • 26
  • 37
  • Probably i was not clear enough...I don not need to commit the transaction, close the connection etc. This is handled by Spring (used in a jee container) .It is all done transparently by the framework.What i notice is that the connection used to get the blob is returned to the pool (via monitoring the admin console) and streaming the content of the blob does not require this connection – Cris Mar 13 '13 at 06:45
  • "When all the references to a connection are gone, the connection is returned to the connection pool during garbage collection" That's totaly wrong...garbage connetion has nothing to do with returning the connection to the pool. Calling close on the wrpped connection will return the connection to the pool. – Cris Mar 13 '13 at 07:11
  • Each container might have a mechanism to return leaked conection to the pool...that;s different – Cris Mar 13 '13 at 07:12
  • Please take a look at http://docs.oracle.com/cd/E13222_01/wls/docs81/faq/jdbc.html. At least in Weblogic, garbage collection is related to returning the connection to the pool according to WLS documentation – Lan Mar 13 '13 at 12:39
  • When you say "streaming the content of the blob", do you mean write streaming to the browser or read streaming from the blob? There are two steps based on my understanding of your steps. – Lan Mar 13 '13 at 12:43