JPA + Hibernate + java.util.logging (JDK logging): show sql queries with arguments - sql

We are using JPA with Hibernate, and because we use the java build-in logging for our project, we have routed Hibernate to use java.util.logging (JDK logging) as well (by including slf4j-jdk14-1.6.4.jar). Now there is a problem with showing the actual queries that hibernate/JPA performs. We have added this to our persistence.xml:
<property name="hibernate.show_sql" value="true" />
That helps a bit, showing some more queries when inserting stuff. However, the question marks still remain. Other answers suggest to set the org.hibernate.type to TRACE in order to solve this, but that is a log4j specific suggestion. How can we actually show all arguments that hibernate uses? Thanks!

Alternative way is to use JDBC Driver Proxy logging library like log4jdbc
log4jdbc uses the Simple Logging Facade for Java (SLF4J) and supports below logging systems:-
Log4j
java.util logging in JDK 1.4
logback
Jakarta Commons Logging
It support showing SQL Queries with parameters ..here is an excerpt from their documentation
jdbc.sqlonly :- Logs only SQL. SQL executed within a prepared statement
is automatically shown with it's bind arguments replaced with the data
bound at that position, for greatly increased readability

create a logging.properties, add the line org.hibernate.SQL.level=FINEST, and launch your program with -Djava.util.logging.config.file=/path/to/logging.properties VM argument

Related

Conflicting class versions in Apache Flink

I have 2 applications. The first is a Play! Framework (v2.5.1) application. This application's job is to read the aggregated data. The second is an Apache Flink (v1.1.2) application. This application's job is to write the aggregated data.
The error
java.lang.NoSuchMethodError: com.typesafe.config.ConfigFactory.defaultApplication(Lcom/typesafe/config/ConfigParseOptions;)Lcom/typesafe/config/Config;
This is caused by Play & Flink using different versions of com.typesafe.config (1.3.0 vs 1.2.1).
I've tried
I've tried using shading, but there are further complications when I get to using Akka. Akka also has conflicting versions, so I shade config & akka, which leads to a configuration error in Akka. If I duplicate the configuration to the correct path, then the ActorSystem fails to initialize because of incorrect class version.
Research
I don't know this area well, but it seems like a number of JVM servers handle this by doing parent-last class loading. Is that possible in flink?
There may be other, simple solutions that I've not tried as well. If there are some of those, let me know, and I'll gladly try them.
Thanks for your help!

Logging SQL queries for specific files in Grails

I am trying to make SQL queries output into the log in my grails app, and I found out how to do so here:
However, I want SQL logging for only specific files in my project.. how can I do this?
Unfortunately, that is not possible. The grails loggers are turned on and off by class or package name of the code doing the logging. In this case, these are core Hibernate and / or Grails classes, so they either log all activity or no activity.
What you can do is add your own logging statements in your code before and after the operations you are interested in. Then you can use grep to find your marker statements in the log file. The SQL logging you are interested in will be in between your markers and you can ignore the rest of the very large log file.

EclipseLink and GlassFish: initial DDL creation

I try to use Flyway to update my databases. In order to setup Flyway I need to get the current DDL. Normaly one would use EclipseLinks schema generation mechanism by configuring:
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
<property name="eclipselink.ddl-generation.output-mode" value="sql-script" />
But I am unable to get this working using a GlassFish 3.1.2.
Is there any possibility to achieve what I want, or am I on the wrong track?
Glassfish overrides the eclipselink DDL properties for its own DDL generation feature described here http://docs.oracle.com/cd/E18930_01/html/821-2418/gbwlh.html
It will force writing out scripts that it then uses to create tables, and drop them if required. I don't have the location it has them written to, but check if you can control it through glassfish. Otherwise, try specifying the "eclipselink.create-ddl-jdbc-file-name" property to define the file name and location it ahold be written to. You can also access the persistence unit in a simple java main class outside glassfish if you need the script just once and want to store it with the persistence unit.

How do I configure persistence.xml for ORM on glassfish v3 with Derby and Eclipselink

I'm using the internal glassfish 3.1 plugin for Eclipse, along with a derby database I installed (it shows up on the datasource explorer in the Database Developer view in Eclipse), and I'm fumbling at the "last" step of getting the ORM working so that I can develop an app that persists data with EJBs using Eclipselink for the JPA implementation.
I know I need to configure the persistence.xml file, but I'm at a loss for what needs to be in it, what the individual field names mean. I feel like the purpose of the persistence.xml is to tell Glassfish where to find the database to store everything in, and which JPA implementation to use to do that storing.
I have a bunch of questions.
Do I have to have a persistence entry for each class that represents an object in the database? So if I had a Book class and a Library class, would I need two enteries in persistence.xml or could I just do one case that services them both?
Where can I find more information about how to configure the persistence.xml file IN GENERAL. I have found tons of very specific tutorials with information on how to configure it in X, Y, or Z setting, but nothing that explains the individual bits, and how you'd configure them from a high level.
Once I've setup my persistence.xml file correctly, what else do I need to do to ensure that my #annotated classes are going to be serviced by the ORM implementation correctly? Is there something I need to configure in Glassfish?
I'm not an expert but...
1) Yes, in my experience you need an entry for each class. There could be exceptions to this but I am unfamiliar with them.
2) [http://wiki.eclipse.org/EclipseLink/] is a good place to start.
[http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Configuration/JPA/persistence.xml] has some details that you may already know. I've have trouble finding a perfect resource myself. I've tended to find information fragmented all over the place.
3) In general most of my persistence.xml file has been generated automatically by eclipselink.
After I creating a connection pool and JDBC resource from the glassfish Administration Console
I had to add my
<jta-data-source>jdbc/your_name</jta-data-source>
to persistence.xml.[1]
<property name="eclipselink.ddl-generation" value="create-tables"/>
<property name="eclipselink.ddl-generation.output-mode" value="database"/>
I added these properties so my identity columns would auto-increment using JPA.
Try these two tutorials to get a better understanding of how it works:
[1] http://programming.manessinger.com/tutorials/an-eclipse-glassfish-java-ee-6-tutorial/#heading_toc_j_24
http://itsolutionsforall.com/index.php
[*apologies I can't post more than 2 links at the moment]

How do I add Retry Logic in NHibernate to handle Transient Failures in SQL Azure?

We have a .NET application that runs on Windows Azure and uses NHibernate to connect to a SQL Azure database. Sometimes it's necessary to add retry logic to handle transient failures in SQL Azure as described for example here -
http://social.technet.microsoft.com/wiki/contents/articles/retry-logic-for-transient-failures-in-sql-azure.aspx
Can someone point me to a way in doing this with NHibernate? Ideally I'd like to do this at the NHibernate level and not wrap every single call; I was able to do this for another ORM, .netTiers (http://nettiers.com) as I outline here -
http://blog.ehuna.org/2010/01/how_to_stop_getting_exceptions.html
I did search and found some answers that mention using a custom implementation of the IDbCommand interface -
Intercept SQL statements containing parameter values generated by NHibernate
But I'm not sure this works with NHibernate 3.2 and I'm looking for a clear example I could modify.
How could I make NHibernate retry calls to SQL Azure automatically? Let's say 3 retries, with 100ms wait between each - after the 3 retries, if still failing, we should throw the exception.
I've released a library that takes care of this:
https://github.com/robdmoore/NHibernate.SqlAzure
This is not a complete running example, but the files you need are here
https://github.com/craigvn/NHibernateRetryable
Maybe this can help you out:
http://www.objectreference.net/post/NHibernate-and-Execution-Plans.aspx. Here there is a class that you can download and play with it, with some overrides.
Also check this link as well: http://elliottjorgensen.com/nhibernate-api-ref/NHibernate.Driver/SqlClientDriver.html
I hope it helps in anyway, I'm going thru a similar situation.
Regards,