1

Normally we check if replication on slaves is working via the console command

SHOW SLAVE STATUS \G;

I would like to incorporate this functionality into a servlet reporting application. However, hibernate does not seem to allow this :

createSQLQuery("SHOW SLAVE STATUS");
...executing query...
java.lang.UnsupportedOperationException: not yet implemented for SQL queries

I'm sure it's possible to achieve this using native JDBC, however maybe there is a better way? Environment: Linux 2.6.18, Tomcat 6/7, Hibernate 3.4, Java 1.6, MySQL 5 Note: I'm not interested in solutions where we insert timestamps on master.

3
  • Where exactly you are getting this exception
    – PSR
    May 30, 2013 at 10:11
  • refer this link you may get some idea about your problem Link. also refer other link from this link May 30, 2013 at 13:09
  • @PSR java.lang.UnsupportedOperationException: not yet implemented for SQL queries at org.hibernate.impl.SQLQueryImpl.getReturnTypes(SQLQueryImpl.java:249) com.xxxxxx.servlet.doGet(AbstractServlet.java:516) May 31, 2013 at 12:36

4 Answers 4

4
+25

When you use createSQLQuery to run a native SQL query, you have to tell Hibernate how to convert the results into Java objects. The easiest way in your case is to add

query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);

to the query before executing it. (If the query returned already-mapped Entities, you could just use addEntity().)

2
  • If this doesn't work then you need to provide more information about what you are doing and what is causing the exception. ...executing query... is not very informative.
    – Old Pro
    Jun 6, 2013 at 17:15
  • Somebody edited my question - apologies if they made it less informative. Jun 7, 2013 at 13:14
3

I haven't tried hard enough to make this work with Hibernate, but if that fails, you can still use a good old-fashioned JDBC connection:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Test {

    static final String DB_ADDRESS = "localhost";
    static final String DB_NAME = "mysql";
    static final String DB_USER = "root";

    public static void main (String[] args) {
        // get password
        String password = "";
        if (args!=null && args.length>0) {
            password = args[0];
        }

        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } catch (Exception ex) {
            // handle the error
            System.out.println("Driver issue: " + ex.getMessage());
            System.out.println("Driver issue: " + ex.getClass().toString());
        }
        // connect to database
        try {
            Connection conn = 
               DriverManager.getConnection("jdbc:mysql://"+DB_ADDRESS+"/"+DB_NAME+"?autoReconnect=true",DB_USER,password);

            // Do something with the Connection
            System.out.println("Connection: " + conn);
            Statement stmt = conn.createStatement();


            ResultSet RS = stmt.executeQuery("SHOW TABLES");
            while (RS.next()) {
                System.out.println("table: '" + RS.getString(1) + "'");
            }

            // disconnect from database
            conn.close();
            stmt.close();

        } catch (SQLException ex) {
            // handle any errors
            System.out.println("SQLException: " + ex.getMessage());
            System.out.println("SQLState: " + ex.getSQLState());
            System.out.println("VendorError: " + ex.getErrorCode());
        }
    }
}

This would return:

Connection: com.mysql.jdbc.JDBC4Connection@528acf6e
table: 'columns_priv'
table: 'db'
table: 'event'
table: 'func'
table: 'general_log'
table: 'help_category'
table: 'help_keyword'
table: 'help_relation'
table: 'help_topic'
table: 'host'
table: 'ndb_binlog_index'
table: 'plugin'
table: 'proc'
table: 'procs_priv'
table: 'proxies_priv'
table: 'servers'
table: 'slow_log'
table: 'tables_priv'
table: 'time_zone'
table: 'time_zone_leap_second'
table: 'time_zone_name'
table: 'time_zone_transition'
table: 'time_zone_transition_type'
table: 'user'

Needless to say, the key thing is to ensure your user has sufficient privileges.

2

This works for me..

SQLQuery query =session.createSQLQuery("show slave status");
ArrayList<Object[]> results = (ArrayList<Object[]>)query.list();
//there is only one row returned
Object [] result =  results.get(0);
//Seconds Behind Master should always be the last value in the row.
String secondsBehindMaster = ""+result[result.length-1];

Ensure DB user has REPLICATION CLIENT permission to make this query.

GRANT REPLICATION CLIENT ON *.* TO 'db_user'@'db_server' IDENTIFIED BY 'db_password';
1
  • Seems so Simple. I think the permissions was also a factor. It now works perfectly. Thanks. Jun 26, 2013 at 17:03
2

As the message of the UnsupportedOperationException exception suggests, getReturnTypes() is not implemented by the SQLQuery object returned by createSQLQuery().

By the way, the phrasing of your question is misleading, you do not receive the exception on calling createSQLQuery(), but a few lines later.

When you issue native SQL, you will retrieve rows as a list of generic Object[] (unless you explicitely provide Hibernate with a mapping). Typically, the types will be known at compile-time.

See here for more information.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.