The only difference between your two cases is that you're closing the database connection. This is usually not a good idea because it drops the page cache.
Closing the connection might be useful only if you have an extremely small amount of memory and really need it for something else.
The important thing is to put many inserts into a single transaction, but you're already doing that.
The question is how many inserts belong into a transaction.
If you can do the receiving and the database operations in parallel, then you should simply insert all data that you've received since the last batch. (This will result in a continous stream of database operations.)
Caching data for a longer timer does not make sense unless you want to avoid doing too much disk I/O.
If you have certain performance requirements, then you must measure yourself.
INSERTstatement with a zillionVALUESentries, then that's almost certainly not the way to go. Neither would be lots of implicit-transactionINSERTstatements. That leaves prepared-statement-inserts in a batched transaction, in which case whether the source of the data comes from "the incoming stream" or a cache probably doesn't make much difference. - TripeHound