INSERT LOW_PRIORITY or INSERT DELAYED in Hibernate
How do I do a low_priority or delayed insert into a MySQL table with Hibernate?
In my logging routine I want to insert the log info to a table in my database for further analysis. But I don't care about how long it takes for the insert to be done, so usually I would say INSERT LOW_PRIORITY INTO LogEntry (level, title, full) VALUES ('Info', 'Title here', 'Full log'); If I have an entity LogEntry, how do I write开发者_开发百科 or wire up my LogEntryDAO to do LOW_PRIORITY or DELAYED inserts and updates?
Cheers
Nik
There is a much simpler solution.
Annotate my entity class with @SQLInsert(sql="INSERT LOW_PRIORITY INTO LogEntry (level, title, full) VALUES (?, ?,?)")
Nice and elegant :-)
Use an org.hibernate.Interceptor
. It will get a copy of the SQL to execute to modify.
See the docs for details.
I would say that a better solution would be to have a Log4J appender write that message to a queue and a MessageListener pick it up and INSERT it into the database.
Your app is decoupled from the database that way, and you won't care how long it takes. If your database goes down, your persistent queue can accumulate messages until you're able to bring it back up again.
Be sure to use an XA JDBC driver and a transaction manager so the queue and database can be one single transaction.
精彩评论