Search code examples
sql-serverhibernatedeadlockdatabase-deadlocks

Forcing Hibernate not to use sp_execute for non-query


Is there a way to force Hibernate not to use sp_execute?

Consider this code

String queryString = "SET DEADLOCK_PRIORITY LOW;";
SQLQuery query = session.createSQLQuery(queryString);
return query.executeUpdate();

It turns into sp_prepare, then sp_execute and that defeats the purpose: after executing this statement session deadlock priority is back to NORMAL

In .NET the following code would execute the statement directly and priority for the session stays LOW (as desired)

command = new SqlCommand("SET DEADLOCK_PRIORITY LOW", _Connection);
command.ExecuteNonQuery();

How to force Hibernate to send the statement in the same manner?


Solution

  • I found a way to accomplish that.

    String statementText = "SET DEADLOCK_PRIORITY LOW;";
    Statement s = session.connection().createStatement();
    s.execute(statementText);
    

    However, connection() is "deprecated and scheduled for removal in Hibernate 4.x" (discussed here)