java - How to track database changes dynamically using spring? -
i using spring
,spring-data
,spring-cronjobs
, java-mail
. have requirement of instantly scanning changes in table present in my-sql
database , fire mail admin regarding changes.
all doing achieve running cronjob scan changes in table, heavy process table related monetary transaction , consumes lot of resources result application becomes slow.
so, there better process can track current changes in database. example if there method set watchers in spring trigger process on database change helpful.
the following sample of entity of table scanning.
/** import statements **/ @entity public class userwallettransaction { @id @generatedvalue private long id; private string toaccount; @manytoone(fetch = fetchtype.lazy) user user; @manytoone(fetch = fetchtype.lazy) wallet wallet; private string senderormobile; private string benificiaryname; private string benimobile; private double transferamount; private double stax; private double charge; private double netamount; private string apitid; private string apicomment; private string agentid; private double apistax; private double apicharge; private double apinetamount; private double apibalanceamount; private string transactionmode; private string bankname; private string usertrackid; private string referencenumber; private string operatordescription; private string mobilenumber; private string rechargedatetime; private string operatortransactionid; private string hermespnr; private string utid; private string status; private double previousamount; private double balanceamount; private double amounttransacted; private string transactiontype; private boolean israised; private boolean isrefunded; @column(name = "created_by") private string createdby; @column(name = "created_date") private date createddate; @column(name = "updated_by") private string updatedby; @column(name = "updated_date") private date updateddate; private string operationperformed; @onetoone private userwallettransaction relationaltransaction; private string errorcomments; @onetoone private user debituser; @onetoone private user credituser; @onetoone private serviceprovider serviceprovider; @onetoone private refundrequest refundrequests; ..... getters , setters...... }
and following sample cron job using fire mail after scanning database.
/** import statements **/ @component @enablescheduling public class validatecron { @autowired rechargeservice rechargeservice; @scheduled(fixeddelay = 2) public void demoservicemethod() { // perform tasks related scan , track number of records added //after previous scan. fire mail if change in database found. } }
please share method can improve performance of functionality. in advance.
hibernate has interceptor mecanism allows notified, @ specific times, when database events occurs.
such events creation/deletion/flush of session. access objects being subject given event, have mean fire process when given object of given class (which can map table in schema) modified.
the javadoc can found here : https://docs.jboss.org/hibernate/orm/4.0/manual/en-us/html/events.html
apart use case, once used interceptors in oracle based, partitionned schema. idea use technical date partition of our tables, , challenge have same "partition date" objects in given tree (if had used sysdate @ time of insertion, nothing prevent 1 part of object tree have partition date @ day n , rest of tree @ day n+1... worrysome @ other levels). interceptor used, then, propagate same date along object tree on insertion.
Comments
Post a Comment