As I understand stream can be read
only once.
Yes
So I need to store whole image in
memory?
Probably, but not necessarily. Look at setBinaryStream method of PreparedStatement. Under some conditions you could probably just pass your input stream to that method. Look at some other stream method, your JDBC driver may only support a subset of those.
With some clever bufferization you may probably serve original stream and resized image as well in a single call.
Is it expensive for performance?
What performance you are talking about. If memory is limited this of course going to matter. Otherwise memory I/O is the least probable place to become a bottleneck.
If you have enough memory, I would recommend you to take this approach, as the implementation is the most straight forward and memory is cheap.
Or there are another way?
As I said before, you may create a clever bufferization technique, but this requires a lot of testing ( and designing ). If memory is limited, you may have to revert to this approach, otherwise, just read the whole stream in, process it and write the result to the DB.