Java – you want to use transactions in Hibernate queries to delete existing documents from folders and database values in tables

I have a file upload application After uploading the file, create a new document path and save the document in the file path At the same time, the document path and related values are saved in the database table After uploading in my application, there is a button to delete unwanted files Sometimes file deletion does not work properly Therefore, there will be a waste of memory I want to avoid this by using transaction statements I don't know how to use hibernate transactions for my work Is that possible? Please help me finish the work successfully (I use spring to integrate with hibernate and PostgreSQL)

thank you

In controller

int supDocId=1102;
String docPath=D:/;
String filePath=docPath+supDocId+".pdf";
File file=new File(filePath);
boolean isDelete = servicesService.deleteDocument(supDocId);
if(isDelete)
{
if(file.exists())
    {
        file.delete();
    }
alertMsg = "The file is deleted sucessfully";   
    }
   else{
   alertMsg = "Deletion Failed.!!!  File is under processing..";    
}

In service class

public boolean deleteDocument(int supDocId){
    return servicesDAO.deleteDocument(supDocId);
}

In servicesdao class

public boolean deleteDocument(int supDocId){
int deleteStatus=0;
try {
  String deleteQuery = "DELETE FROM  tablename  WHERE attch_doc_id='"+supDocId+"'";
 Query deleteQ = session.createsqlQuery(deleteQuery);
 deleteStatus  = deleteQ.executeUpdate();                                       
 if(deleteStatus>0){    
   return deleteStatus;         
        }
    } catch (Exception e) {
        e.printStackTrace();
            }
        return deleteStatus;    
}

I want to use transaction statements to handle two operations (document and database value deletion) in the Dao class

Solution

If the database transaction fails – you will not only encounter the problem of deleted files, but also the problem of uploaded new files

In most cases, the file system does not support transactions, so I don't think a bulletproof solution can be achieved by using Xa (distributed transactions using JTA) or similar methods

The fairly straightforward solutions I use in some projects:

>Files in the file system are created before the database is committed (but preferably after hibernate refresh). > In the file deletion operation, the reference to the file is deleted from the DB, but not from the file system. > Asynchronous processes regularly scan all files in the file system and delete files without DB references

If you have a large number of files, you may need to make some optimizations to prevent all files from being scanned all the time For example:

>Put the new file into the folder named "current date", so only some files can be checked for unsuccessful transactions to upload the new file. > In the file deletion operation, refer to the file to be deleted and insert the new record into the table "deleted_files" > Create an asynchronous process to periodically scan the table "deleted_files" and delete physical files. If the deletion is successful (or the file has been lost), delete records from the table "deleted_files" and commit transactions

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>