1

I am trying to create events using Java code with hibernate. I verified my syntax for CREATE EVENT in MySQL Workbench, and I verified my Java code with a simple UPDATE query. But when I am trying to use both it just doesn't work. I am not getting any error message, just that 0 rows were affected. My code is as follows:

String sql = "CREATE EVENT IF NOT EXISTS test_event ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 20 SECOND ON COMPLETION PRESERVE ENABLE DO UPDATE my_table SET last_error_message='my test' WHERE ID=17;";

session.beginTransaction();
SQLQuery query = session.createSQLQuery(sql);
int result = query.executeUpdate();
session.getTransaction().commit();
....
session.close();

thanks a lot

3 Answers3

0
  1. Get jdbc connection from your session: How to get jdbc connection from hibernate session?

  2. Use JDBC to execute DDL

...

Statement stmt = null;
try {
    stmt = conn.createStatement();
    stmt.executeUpdate(sql);
} catch (SQLException e) {
    // ...
} finally {
    // close stmt
}

...

Community
  • 1
  • 1
Multisync
  • 8,657
  • 1
  • 16
  • 20
0

How do you know if any new events were created? You can try

show events from <SCHEME_NAME>; 

This will show all the events that are registered to the given schema.try printing the session\statement warning stack...

Dinidu Hewage
  • 2,169
  • 6
  • 40
  • 51
Gal Mazor
  • 100
  • 2
  • 10
0

First, you need to make sure your database is prepared to execute an event. for that, you need to run the following command.

SHOW PROCESSLIST;

MySQL uses a special thread called event schedule thread to execute all scheduled events. enter image description here

if you see your process list like the above picture. you need to run the below command to enable MySQL event execution.

SET GLOBAL event_scheduler = ON;

Then when you execute the "SHOW PROCESSLIST;" command you will see the below list which shows the specific thread for event execution in MySQL. enter image description here

Now you can create your event using any MySQL client interface.

Dinidu Hewage
  • 2,169
  • 6
  • 40
  • 51