如何使用Spring动态跟踪数据库更改? [英] How to track database changes dynamically using spring?

查看:147
本文介绍了如何使用Spring动态跟踪数据库更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用springspring-dataspring-cronjobsjava-mail.我需要立即扫描my-sql数据库中存在的表中的更改,并向管理员发送有关更改的邮件.

I am using spring,spring-data,spring-cronjobs and java-mail. I have a requirement of instantly scanning the changes in a table present in my-sql database and fire a mail to the admin regarding the changes.

我要做的就是运行cronjob来扫描表中的所有更改,但这是一个繁重的过程,因为表与货币交易有关,并且消耗了大量资源,因此应用程序变得太慢了.

All I am doing to achieve this is running a cronjob to scan all the changes in the table, but it is a heavy process as the table is related to monetary transaction and the consumes a lot of resources as a result the application becomes too slow.

因此,有没有更好的过程可用来跟踪数据库中的当前更改.例如,如果有任何方法可以在春季设置观察者以触发数据库更改过程,则将很有帮助.

So, Is there any better process by which I can track the current changes in the database. For example If there is any method to set watchers in spring to trigger a process on database change it would be helpful.

以下是我正在扫描的表的实体的示例.

The following is a sample of the entity of the table I am 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 and setters......
}

以下是我在扫描数据库后用来激发邮件的示例cron作业.

And the following is a sample cron job I am using to fire the mail after scanning the database.

/** Import statements **/
@Component
@EnableScheduling
public class ValidateCron {
    @Autowired
    RechargeService rechargeService;
     @Scheduled(fixedDelay = 2)
     public void demoServiceMethod() {
        // Perform tasks related to scan and track the number of records added //after the previous scan. Fire a mail if any change in the database is found. 
     }
}

请分享我可以用来改善此功能性能的任何方法.预先感谢.

Please share any method by which I can improve the performance of this functionality. Thanks in advance.

推荐答案

Hibernate具有拦截器机制,可让您在特定时间发生数据库事件时得到通知.

Hibernate has an interceptor mecanism that allows you to get notified, at specific times, when database events occurs.

此类事件是会话的创建/删除/刷新.当您访问受给定事件影响的对象时,就有一种方法可以在修改给定类的给定对象(可以轻松映射到架构中的表)时触发一个过程.

Such events are creation/deletion/flush of the session. As you get access to the objects being subject to the given event, you have a mean to fire a process when a given object of a given class (which you can easily map to a table in your schema) is modified.

可在此处找到javadoc: https ://docs.jboss.org/hibernate/orm/4.0/manual/en-US/html/events.html

The javadoc can be found here : https://docs.jboss.org/hibernate/orm/4.0/manual/en-US/html/events.html

除了您的用例之外,我曾经在基于Oracle的分区模式中使用拦截器.想法是使用一个技术日期来对某些表进行分区,而挑战在于给定树中的所有对象都具有相同的分区日期"(如果我们在插入时使用了SYSDATE,则没有什么可以防止的)对象树的一部分在第N天有一个分区日期,而树的其余部分在第N + 1天……这在其他一些级别上令人担忧.然后,使用拦截器在插入时沿对象树传播相同的日期.

Apart from your use case, I once used interceptors in an Oracle based, partitionned schema. The idea was to use a technical date to partition some of our tables, and the challenge was to have the same "partition date" for all the objects in a given tree (if we had used SYSDATE at the time of insertion, nothing would prevent one part of the object tree to have a partition date at day N and the rest of the tree at day N+1... which is worrysome at some other levels). An interceptor was used, then, to propagate the same date along the object tree on insertion.

这篇关于如何使用Spring动态跟踪数据库更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆