Is there way to record audit database in spring boot using Spring Data Envers with spring JDBCTemplate?

huangapple go评论154阅读模式
英文:

Is there way to record audit database in spring boot using Spring Data Envers with spring JDBCTemplate?

问题

我正在创建一个使用Spring的Web应用程序,该应用程序使用MySQL数据库和Spring的JDBCTemplate进行操作。问题是我想要记录存储在MySQL数据库中的任何数据更改。我无法找到在使用JDBCTemplate时使用Spring Data Envers来记录这些更改的解决方案。

记录数据库中数据的任何更改的最佳方法是什么?或者只是在Spring应用程序中编写一个文本文件吗?

英文:

I'm creating a spring web application that uses the MySQL database with spring JDBCTemplate. The problem is I want to record any changes in data that store in the MySQL database. I couldn't find any solution for Spring Data Envers with JDBCTemplates to record the changes.

What is the best way to record any changes of data of database? or by simply writing a text file on the spring app?

答案1

得分: 1

Envers 是 Spring Data Envers 构建的基础,它是 Hibernate 的附加组件,并利用其变更检测机制来触发将修订写入数据库。

JdbcTemplate 则不具备这些功能,它只是通过抽象化异常处理或遍历查询的 ResultSet 等重复任务,简化了执行 SQL 语句的过程。JdbcTemplate 不知道它执行的语句实际上在做什么。

因此,你有几个选择:

  1. 在数据库上放置触发器以记录更改。
  2. 使用某些依赖于数据库的功能,比如 Oracle 的变更数据捕获(Change Data Capture)
  3. 你可以创建一个 JdbcTemplate 的包装器,分析 SQL 语句并生成日志条目。这只在你仅需要非常有限的信息时才可行,比如执行了什么类型的语句以及影响了哪个表。
  4. 如果你需要更多语义信息,最好使用应用程序堆栈中更高级别的部分(如控制器或服务)来收集相关信息并将其写入数据库。可能也会使用 JdbcTemplate
英文:

Envers on which Spring Data Envers builds is an add on of Hibernate and uses it's change detection mechanism in order to trigger writing revisions to the database.

JdbcTemplate doesn't have any of that, it just eases the execution of SQL statements by abstracting away repetitive tasks like exception handling or iterating over the ResultSet of queries. JdbcTemplate has no knowledge of what the statement it is executing is actually doing.

As so often you have a couple of options:

  1. put triggers on your database that record changes.
  2. use some database dependent feature like Oracles Change Data Capture
  3. You could create a wrapper of JdbcTemplate which analyses the SQL statement and produces a log entry. This is only feasible when you need only very limited information, like what kind of statement was executed and which table was affected.
  4. If you need more semantic information it is probably best to use an even higher level of your application stack like the controller or service to gather the relevant information and write it to the database. Probably using the JdbcTemplate as well.

huangapple
  • 本文由 发表于 2020年9月30日 16:15:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64133520.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定