0

I am facing a error while uploading image to database, i am passing byte array ,but getting Exception below. could anyone help me `@Override @Transactional @SuppressWarnings("unchecked") public void update(MyFiles eqFiles, byte[] imageBytes) {

    int result=sessionFactory.getCurrentSession().createNativeQuery(FILE_UPDATE).setBinary("image", imageBytes)
            .setInteger("id", eqFiles.id().setTimeout(60).executeUpdate();
    
}` 

Exception as follows:

SEVERE: Servlet.service() for servlet [jersey-servlet] in context with path [/HFD] threw exception [org.springframework.transaction.TransactionSystemException: Could not roll back Hibernate transaction; nested exception is org.hibernate.TransactionException: Unable to rollback against JDBC Connection] with root cause java.sql.SQLException: Connection is closed

Any responses would be appreciated, thanks in advance.

Shkfar
  • 23
  • 1
  • 8
  • Have you tried doing `openSession` instead of `getCurrentSession`? Maybe the issue is that your session has been closed somewhere else. Have you tried checking if the session is open with `session.isOpen()`? – ferrouskid Feb 15 '22 at 16:59
  • i tried uploading small file like 5 kb, its working, its not working above 10. any suggesions for timeout?@ferrouskid – Shkfar Feb 16 '22 at 06:41
  • How are you handling the upload at the moment? Personally I think its a good idea to have it upload to the backend first where it can store it locally, and then the backend can open a session and start a transaction in the database. The reason I think this may be useful is because internet speeds may be slow and vary, so if you start the database transaction once you have the entire file locally, you are independent of the internet connection quality. If the problem still persists, you could try increasing timeout limit, or store media files separately (not in a database). – ferrouskid Feb 16 '22 at 13:04
  • I think storing files in a database isn't the best approach. Check out this [answer](https://stackoverflow.com/a/3751/17105581), and have a read of this [article](https://maximorlov.com/why-storing-files-database-bad-practice/) and this [article](https://dev.to/guilhermetoti/are-you-storing-your-files-correctly-let-s-explore-a-real-world-backend-issue-2adf). – ferrouskid Feb 16 '22 at 13:08
  • its based on requirement totally. what i don't understand is that i am having issues with images, pdf, doc files and other types work fine even large. @ferrouskid – Shkfar Feb 17 '22 at 04:18
  • Right, I getchya. What happens when you call `setBinary(int position, byte[] val)` instead, does it still cause an issue? How about the PDF code, could you show the code there? Final question, how are you storing the images in the database, are you using `BLOB`? – ferrouskid Feb 17 '22 at 13:20
  • in database its varbinary(max) and in client side sending it as byte array. i tested few more files. what i found is weird, having issue with images above 8 kb , getting above error ,otherwise works fine. i tested .doc nearly 1 mb , doesn't faces issues. not sure why it behaves different based on filetype,while i dont have any logic for that. @ferrouskid – Shkfar Feb 18 '22 at 11:00
  • Hmm, interesting. I had a look online, and different databases have different `varbinary(max)` sizes, but they all still should be above 8kb. Could you try using `BLOB` type instead (binary large object) in your database? I think you could then use `@Lob` [annotation](https://docs.oracle.com/javaee/6/api/javax/persistence/Lob.html) – ferrouskid Feb 19 '22 at 13:40
  • Ah, found [this](https://vladmihalcea.com/query-timeout-jpa-hibernate/). Apparently the timeout is in milliseconds. 60 milliseconds I think is a bit too little. Can you try 6000 milliseconds instead, that seems like a reasonable timeout? – ferrouskid Feb 19 '22 at 13:48

0 Answers0