How to capture log for MYSQL event scheduler - mysql

I am new to MYSQL. I have one event scheduler for every 1 hour. Can we create log for every call when it was started and when it was finished and the status (success or failure).
Pls help me.

You can catch the errors with
DECLARE EXIT HANDLER FOR SQLEXCEPTION
or you can use GET DIAGNOSTICS which is available in MySQL 5.6.4
Make a log table and insert timestamps and error returned by handler

Related

Handle Abort/Kill/Connection Close from Stored Procedure

I'm writing a MySQL stored procedure that does maintenance on large tables, and runs every night.
Unfortunately, due to reasons beyond my control, the running SP sometimes stops unexpectedly (due to "kill" by another admins or dropped connections).
Is there a way to "catch" those situations from within my SP and update a table (e.g. an activity log or maintenance audit table) when this happens?
I tried:
DECLARE EXIT HANDLER FOR SQLWARNING, SQLEXCEPTION
...and other specific ERRORs and SQLSTATEs:
DECLARE EXIT HANDLER FOR 1078, 1080, 1152, 1159, 1161, 1184, 1317, 3169, SQLSTATE '08S01'
but non of them seem to catch when aborting or killing.
DECLARE EXIT HANDLER is for times your query terminates normally, something like clicking on shutdown button in windows. But kill process by another admin is something like reset PC by hand or power outage, So there is no time to do anything.
A good way to find these situations is investigating MySQL logs. If you cannot access to logs, try to do that by yourself. For example create a log table (include id, sp_name, start_time,end_time) and insert a record every time SP starts. You can do it by inserting timestamp and SP name at the beginning of SP. At the end of SP, you can update this record by inserting a timestamp for end_time. Every record with a start_time and without end_time means the contained SP name is killed by others after start_time time.

How to check mysql event status

How to check event scheduler status in MySQL database?
I want to know the running status of my event whether it is running or not?
how to check event scheduler status
It can be ON or OFF (TRUE/FALSE, 1/0) and can be viewed by
SELECT ##GLOBAL.event_scheduler;
-- or
SELECT ##event_scheduler;
for example.
i want to know the running status of my event whether it is running or not
This is absolutely another task. Event scheduler != Event.
Look at
SHOW PROCESSLIST;
for your event executing thread row is present or not.

How to set MySQL Events to not trigger if last event is still executing

I normally set MySql Events as follows, where UpdateTable() is a stored procedure which changes the table data.
CREATE EVENT UpdateTable_Every1Mins -- Create and Event
ON SCHEDULE EVERY 1 MINUTE STARTS CURRENT_TIMESTAMP + INTERVAL 1 MINUTE
DO CALL UpdateTable();
Occasionally, the UpdateTable procedure is time consuming and can span up to 3 minutes. Since I've set the interval to 1 minute (as shown in the query above), how do I ensure that a new UpdateTable call is not triggerred when the current UpdateTable call is still running?
Thanks
You can't, directly.
Indirectly, sure.
Inside the procedure, before any other work:
IF GET_LOCK('my_lock_name',0) IS NOT TRUE THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'failed to obtain lock; not continuing';
END IF;
Right before the end of the procedure:
DO RELEASE_LOCK('my_lock_name');
This is a named lock. It doesn't lock rows or tables, it just locks the name 'my_lock_name' (a name you make up) in a global memory structure so that no other thread can lock the same name. The 0 means not to wait around for the lock but to immediately return false (0) if someone else holds the lock or null if an error occurs -- and IS NOT TRUE matches either null or 0. See GET_LOCK() in the manual. The above code works in MySQL Server 5.5 or later. Earlier versions support the locks, but do not support SIGNAL to halt execution.
Releasing the lock is not technically necessary with events, since it's released when the client thread terminates, and each event invocation is in its own thread, which terminates when the event invocation ends... but it's best practice.
SIGNAL throws an exception, which stops execution of the procedure and of this invocation of the event. The message_text is logged to the MySQL error log.

Is it possible to rollback a CREATE EVENT in MySQL?

My transaction's sql statements look something like this:
Begin a transaction with START TRANSACTION
Create a record in TEST table
Start a mysql event with CREATE EVENT, using that record's id.
Update a TEST_2 table
Commit the transaction with COMMIT
Of course, if it fails, then I do a ROLLBACK, but after testing, I noticed that the newly created EVENT isn't getting rolled back to nonexistence.
I then found out that CREATE EVENT triggers an implicit COMMIT, in here https://docs.oracle.com/cd/E17952_01/refman-5.1-en/implicit-commit.html
So, is there any way I can automate the event rollback process? Or somehow make it so that the event gets rolled back if the transaction fails?
Thanks.
May triggers do the job? Triggers are designed to execute in response to a specific event that occur in the database.
Oracle Trigger reference

know on which table 'begin tran' has happened or find which table is locked / deadlocked

I am new to c# and sql server. I'am using SQL Server 2008 and I have 126 tables in my database.
There are 7 transaction tables on which insert/update query fires frequently as there are 30-40 users for my application.
I have written BEGIN TRAN before every query and COMMIT at the end of the query.
Now many-a-times I get error 'Timeout expired ...' when any random user tries to open a form or save some data.
I have written ROLLBACK in my triggers if the trigger throws an error.
But I could not identify on which table BEGIN TRAN has happened or which table is deadlocked.
I have made sure that my connection is proper and is open, then I'am getting this error too
and I couldn't identify from where it is comming.
Does anyone have any idea from where this 'Timeout expired' error is comming and can suggest me some way out?
Does any one have any idea from where this 'Timeout expired' error is coming and can suggest me some way out.
One of the possible reasons is that the transaction cannot acquire lock on some resource(table, row, ...).
In that case you may try to increase the LOCK_TIMEOUT or change the isolation level(if acceptable).
I would suggest reading this article.