0

im building a chat module for my site and I want to only show the users 50 last chats and when I add more than 50 delete the first chat and so on.

I made it work with the SpringScheduler. I check every 5 mins and if the database has more than 50 records I delete the all except last 50. is there a better way to do this?

EDIT

im using 10.3.16-MariaDB (SQL) and Spring boot 2.2.6.

I tried to add Triggers like this Answer but I'm getting this exception:

java.sql.SQLException: Can't update table 'chats' in stored function/trigger because it is already used by statement which invoked this stored function/trigger
Rea Teria
  • 133
  • 2
  • 17
  • You can query for last 50 records. – Subir Kumar Sao May 24 '20 at 17:18
  • is it resource-friendly to save all the chats in the database? @SubirKumarSao – Rea Teria May 24 '20 at 17:29
  • Depends on what kind of database you have. No sql database will not sweat. If you are run jobs to remove old records periodically it too will have impact on transaction performance as multiple queries running at same time would slowdown DB.This seems like a design question, that would mean more details is required to give proper advice. – Subir Kumar Sao May 24 '20 at 18:39
  • If you can use stored procedures on db, is way better then create connection from the app, use network etc. Not to mention that what if you want to run multiple instance. But we need more information for better advices. – zlaval May 24 '20 at 18:48
  • @SubirKumarSao I added more information, please see the edit – Rea Teria May 24 '20 at 19:09
  • @zlaval I added more information, please see the edit – Rea Teria May 24 '20 at 19:09

0 Answers0