Want to delete an existing document from the folder and database values ​​from the table using a transaction in a sleep request

I have an application downloading a document. After loading the document, the document path is created again and the document is saved in the file path. At the same time, the document path and associated values ​​are stored in the database table. In my application, after loading, there is a button for deleting an unwanted document. Sometimes the deletion operation does not work properly. There is also memory loss. I want to avoid the situation using a transaction statement. I don’t know, I know how to use a sleep transaction for my work. Is it possible? Please help me get the job done successfully (I use spring with the integration of sleep mode and postgresql)

thanks

In the 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 class of service

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

In the 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 work with two operations (deleting a document and a database) in a DAO class using a transaction statement.

+4
source share
2 answers

If the DB transaction fails, you will have problems not only with the deleted files, but also with the downloaded new files.

In most cases, the file system does not support a transaction, so I don’t think that a solution with a bulletproof version can be achieved using XA (distributed transactions using JTA) or a similar approach.

A fairly direct solution that I use in some of my projects:

  • ( ).
  • , .
  • .

, , . :

  • " ", .
  • "deleted_files" , .
  • "deleted_files" , ( ) "deleted_files" .
0

+supDocId+ ? Sql Injection.

deleteQ.Execute(); - , , true false, - .

0

Source: https://habr.com/ru/post/1617822/


All Articles