Whitelist specific query from hibernate slow query logging - java

I am using below hibernate settings to log slow queries for my application
hibernate.session.events.log.LOG_QUERIES_SLOWER_THAN_MS : <time>
org.hibernate.SQL_SLOW: warn
It is working fine. But I want to whitelist a few queries so that even if they run beyond 'LOG_QUERIES_SLOWER_THAN_MS' will not be logged as a warning. I want to do that as I know there are a few queries which supposed to take a longer time to run and I don't want to generate any alerts for those.
Is there any way I can do that?
I have tried to find any settings I can provide during the query to override the behaviour for that specific query.
Are there any settings and API hibernate provide to achieve it?

There is no way to do that with plain Hibernate. You can just disable the org.hibernate.SQL_SLOW logger temporarily with your logger API by setting the level programatically to e.g. WARN and then reset it again to INFO.

Related

Enabling Hibernate query logging through Java code

I want to print a particular hibernate query in UAT environment. However, I do not want to enable hibernate logging for all the queries since it will start generating huge logs.
Is there a way I can enable hibernate query logging through java code just before the query I want to print and then disable it immediately? Please note that I am using Spring data Jpa repository.
It depends on the logging library that you use.
I think Spring uses SLF4J and logback so something like the following should work:
LoggerContext loggerContext = (LoggerContext)LoggerFactory.getILoggerFactory();
loggerContext.getLogger("org.hibernate.SQL").setLevel(Level.DEBUG);
and afterwards set
loggerContext.getLogger("org.hibernate.SQL").setLevel(Level.ERROR);
There is away to do customized logging with help of logback but not sure it will help to log the particular SQL logs.
Use this link -https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto.logging

How to run mongodb instance in ERROR log level mode?

Not able to run mongodb instance in ERROR log level. As defined by mongodb documentation, by default verbosity is 0 which includes information messages. But increasing verbosity to 1-5 will include debug level messages. I need only error messages to be logged in my log file. I am currently using mongodb-3.6.3 version with java driver at client side.
Is there any way to do it? If yes, how to achieve this? I've already tried reducing logs by adding quiet = true in the config file. But still, a lot of unnecessary logs are generated.
Add this line to your application.properties file and check the console output after running any MongoRepository query.
logging.level.org.springframework.data.mongodb.core.MongoTemplate=ERROR

Capture parameterized string from MyBatis #Select query

I'm running into an issue with a query my Java app is executing against an Oracle DB. Taking the query from exception and replacing the parameters (denoted as ? in the exception log) has it running fine in the DBA tool.
So I want to get a good look at the parameters being set in the final query that's actually being passed to Oracle, and I can't find a clear way to grab that from the #Select annotation.
I've been curious on doing that for a while, so this question is specifically about if it's possible to grab that parameterized query being sent to Oracle, not the specific query or error I'm getting. Appreciate the help.
This is described in documentation
In a nutshell:
configure the logging provider you use in mybatis-config.xml
depending on the provider set TRACE logging level for the mapper you are interested in
For logback provider this is described for example here. For log4j mybatis documentation has more than enough information.

How to determine what SQL query a java web application is using to return the data?

I have been given a java web application for which I have the source code to. The application queries an Oracle database to return data back to the user in web page. I need to update a value for the returned data in the database, without knowing the table or column names. Is there a general way to determine what query the application is submitting to return the data for a particular page so I can find the right tables and columns?
I am new to java and web development so not sure where to start looking.
Thanks!
Well, there's always the old fashioned way of finding out. You can find the source code for the specific page you're looking at and identify the query that's being executed to retrieve the data. I'm assuming that's not what you're looking for, though.
Some other options include using JDBC (Enabling and Using JDBC Logging) logging feature or JProfiler (the JDBC probe shows you all SQL statements in the events view). Once you find the SQL statement, you can use standard text search features within your IDE to locate the specific code and make alterations.
Hope that helps!
If you can run a controlled test (e.g., you are the only person on that web application), you could turn on SQL tracing on the DB connection and then run your transaction several times. To do this
look at all the connections from that application using v$session -- you can control this by tweaking your connection pool setting (e.g., set min and max connection to 1). Assuming this is your test environment.
turn on 10046 trace (see https://oracle-base.com/articles/misc/sql-trace-10046-trcsess-and-tkprof -- there are many other examples).
The 10046 trace will show you what the application is doing -- SQL by SQL. You can even set the level to 12 to get the bind variable values (assuming you are using prepared statements).

How to generically test a database connection with hibernate

I have a service method on an api that can be called to check the health of my database connection.
The method is pulling the query string from a properties file (depends on DB vendor, using Sybase and HSQL for now, more in future), and executing it. Then the method lets the caller know if it succeeded or failed.
In addition to this, I was using the Query.setHint("javax.persistence.query.timeout") to set a timeout on the query:
javax.persistence.EntityManager entityManager;
...
Query heartbeatQuery = entityManager.createNativeQuery(heartbeatQueryString);
heartbeatQuery.setHint("javax.persistence.query.timeout", heartbeatTimeout);
heartbeatQuery.getResultList();
My problem is the timeout property is working against my Sybase DB, but not against my HSQL DB. It sounds like it depends on the vendor, so I don't know for sure when it will work.
Is there a better way to generically test the DB connection & include some kind of timeout parameter?
Well sadly no. JPA's query hints are not mandatory, i.e. it's up to the implementator (EclipseLink, Hibernate, etc) to enforce them or not. Moreover, even if the implementator does chose to recognize a certain query hint, if that hint's functionality is not supported by the database then it won't work (here some implementators are nice and tell you if a certain hint won't work agains the current db while others fail silently). In the case of HSQLDB there's no way to set the query timeout. You can only set a timeout for the login (i.e. how long should it wait for a successful login before failing), but not for the queries duration.
Things are not so grim however. On the one hand, even if you'd solve this, you'd still stumble over other issues with HSQLDB, as it does not support a lot of other nice functionalities that most dbs have. You should only use HSQLDB for basic integration/unit testing. For more involved testing, you can use the integrated MySQL Java library. You can find it here:
http://dev.mysql.com/doc/refman/5.0/en/connector-mxj.html
This is simply a packaged fully working Mysql server, which has a Java api for star and stop, works on most major OSs (win,lin, os x, etc). This way you can have your integration tests start a real Mysql server, and try your code there, where such stuff as a query timeout hint will work fine.

Categories