Adding quartz to project breaks jdbc dataSource - java

I've already found out what was the problem, but decided to post it here, in case anyone else is banging head against the same wall.
I've added the latest version of quartz scheduler to my project:
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
And suddenly tests started falling with:
org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized SQLException for SQL [ my sql ]; SQL state [null]; error code [0]; An SQLException was provoked by the following failure: java.lang.InterruptedException; nested exception is java.sql.SQLException: An SQLException was provoked by the following failure: java.lang.InterruptedException
Even more weird, when I've moved quartz dependency to another module, they started to fail with different exception:
Could not get JDBC Connection; nested exception is java.sql.SQLException: com.mchange.v2.c3p0.ComboPooledDataSource[ identityToken -> z8kfsx9b1qeyobp10iytmq|796c39ad, dataSourceName -> z8kfsx9b1qeyobp10iytmq|796c39ad ] has been closed() -- you can no longer use it.
So, Spring closed my ComboPooledDataSource, but why?
Removing quartz or changing it's version to 1.8.x helps. Exceptions happen even without instantiating any quartz object.

Quartz uses c3p0 jdbc pool for it's distributed job storage since version 2.x, and my project uses c3p0 too, but the newer 0.9.5 version, while the quartz uses 0.9.1.1. These versions are fairly different, the old one from quartz came first in my classpath, causing runtime exceptions. Since I don't use quartz distributed job storage, I've solved the problem by excluding transitive c3p0 dependency:
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
<!-- old c3p0 (0.9.1.1) in class path causes destruction of ComboPooledDataSource-->
<exclusions>
<exclusion>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
</exclusion>
</exclusions>
</dependency>

Related

WebDriverManager throwing TypeNotPresentException

I am trying to replace my local geckodriver.exe with WebDriverManager, but when i invoke
WebDriverManager.firefoxdriver().setup();
i get the following error
4391 [main] INFO io.github.bonigarcia.wdm.WebDriverManager - Reading https://api.github.com/repos/mozilla/geckodriver/releases to seek geckodriver
4704 [main] WARN io.github.bonigarcia.wdm.WebDriverManager - There was an error managing geckodriver (latest version) (Type com.google.gson.internal.LinkedTreeMap not present) ... trying again using latest driver stored in cache
4704 [main] INFO io.github.bonigarcia.wdm.WebDriverManager - Reading https://api.github.com/repos/mozilla/geckodriver/releases to seek geckodriver
4876 [main] ERROR io.github.bonigarcia.wdm.WebDriverManager - There was an error managing geckodriver (latest version) (Type com.google.gson.internal.LinkedTreeMap not present)
java.lang.TypeNotPresentException: Type com.google.gson.internal.LinkedTreeMap not present
at sun.reflect.generics.factory.CoreReflectionFactory.makeNamedType(CoreReflectionFactory.java:117)
at sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:125)
at sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:49)
at sun.reflect.generics.visitor.Reifier.reifyTypeArguments(Reifier.java:68)
...
at io.github.bonigarcia.wdm.WebDriverManager.fallback(WebDriverManager.java:825)
at io.github.bonigarcia.wdm.WebDriverManager.handleException(WebDriverManager.java:802)
at io.github.bonigarcia.wdm.WebDriverManager.manage(WebDriverManager.java:599)
at io.github.bonigarcia.wdm.WebDriverManager.setup(WebDriverManager.java:287)
I am using the latest 5.0.3 WebDriverManager from maven
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.0.3</version>
</dependency>
TypeNotPresentException is thrown when an application tries to access a type using a string representing the type's name, but no definition for the type with the specified name can be found.
It's pretty similar to ClassNotFoundException, but not checked during compilation.
But the root cause is:
com.google.gson.internal.LinkedTreeMap not present in the project classpath.
Try to add gson dependency to the project.
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.0</version>
</dependency>
https://docs.oracle.com/javase/9/docs/api/java/lang/TypeNotPresentException.html
Change the WebDriverManager scope to test in pom.xml as follows:
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.0.3</version>
<scope>test</scope>
</dependency>
or change the scope to compile in pom.xml as follows:
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.0.3</version>
<scope>compile</scope>
</dependency>
Additionally, you may need to add the slf4j dependency as follows:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
As per Driver Management - Feature:
Although not mandatory, it is highly recommended to use a logger library to trace your application and tests. In the case of WebDriverManager, you will see the relevant steps of the driver management following its traces. See for example the following tutorial to use SLF4J and Logback. Also, you can see an example of a WebDriverManager test using logging here (this example uses this configuration file).
Reference
You can find a couple of relevant detailed discussion in:
I am getting an error while executing the basic website launch using web driver manager

Why we are getting exception when we enable the query-cache in hibernate.cfg.xml file by use hibernate 5.3.1.Final dependency?

I created a small second level cache program by using 'hibernate 5.3.1.final' dependency.I used below dependencies to work with Second level cache.
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>5.3.1.Final</version>
</dependency>
In this dependency the 'EhcacheRegionFactory' package name was changed.so we need to use like below property in our 'hibernate.cfg.xml'file.
<property name="hibernate.cache.region.factory_class">
org.hibernate.cache.ehcache.internal.EhcacheRegionFactory
</property>
In hibernate 5.3 the second level cache is working fine but 'query-cache' is not working.i am getting below exception when iam enabling the query-cache in 'hibernate.cfg.xml'.
->Exception in thread "main" java.lang.ExceptionInInitializerError
->Caused by: org.hibernate.service.spi.ServiceException: Unable to create
requested service [org.hibernate.cache.spi.CacheImplementor]
->Caused by: org.hibernate.cache.CacheException: On-the-fly creation of
JCache Cache objects is not supported
[org.hibernate.cache.spi.TimestampsRegion]
How to solve this one?
But The same program when i tried with hibernate 5.2.17 dependency the query cache working fine.
Use Hibernate ORM Hibernate Core ยป 5.3.4.Final dependencies it will work

Maven dependency does not update

I have a problem with Maven.
I used to use the 6.0.5 mysql-connector but because of some serverTime error I wanted to go back to an older version, but after that I receive:
java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver
Everything stays the same only the dependency has changed:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.5</version>
</dependency>
changes to:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
Maven appears to update the jars properly, only somehow the compiler doesn't see them. Any idea what may cause the problem? When I go back to 6.0.5 version no error appears.
From https://dev.mysql.com/doc/connector-j/6.0/en/connector-j-api-changes.html :
The name of the class that implements java.sql.Driver in MySQL Connector/J has changed from com.mysql.jdbc.Driver to com.mysql.cj.jdbc.Driver. The old class name has been deprecated.
So I presume you updated the Driver classname to the cj package when you upgraded to 6.0.5, but you should change it back to com.mysql.jdbc.Driver make things work with 5.1.38.

Camel catching jdbc exception ClassNotFound running on karaf

I'm trying to catch the the exception in my camel route that is thrown when jdbc can't get the connection which is
org.springframework.jdbc.CannotGetJdbcConnectionException
To do this I got the following camel blueprint route
[...]
<doTry>
<to uri="sql:UPDATE log SET field = :#value" />
<doCatch>
<exception>org.springframework.jdbc.CannotGetJdbcConnectionException</exception>
<process ref="exceptionProcessor"></process>
</doCatch>
</doTry>
[...]
But now I get
java.lang.ClassNotFoundException: org.springframework.jdbc.CannotGetJdbcConnectionException
I already tried to add the dependency
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.3.RELEASE</version>
</dependency>
in my pom.xml or to import the package in the maven-bundle-plugin
<Import-Package>org.springframework.jdbc.*;*</Import-Package>
which both didn't work.
I only can catch 'java.lang.Exception' now. How can I do this with the
org.springframework.jdbc.CannotGetJdbcConnectionException
Exception?
You are missing to connector?
Download it here for mysql
http://dev.mysql.com/downloads/connector/j/
Even though you imported jbdc from maven you still need to include the connector in maven the mysql connector is, (no need to download the link above if you use maven like below)
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>
If you aren't using mysql there are other dependencies you need for connecting to different types of databases.

Trouble starting Hibernate Validator due to Bean Validation API

I'm trying to use Hibernate Validator in my project, but it isn't working. On the following line:
SessionFactory sessions = config.buildSessionFactory(builder.build());
I get the following exception:
org.hibernate.cfg.beanvalidation.IntegrationException: Error activating Bean Validation integration
at org.hibernate.cfg.beanvalidation.BeanValidationIntegrator.integrate(BeanValidationIntegrator.java:154)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:311)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1857)
at net.myProject.server.util.HibernateUtil.<clinit>(HibernateUtil.java:32)
... 36 more
Caused by: java.lang.NoSuchMethodError: javax.validation.spi.ConfigurationState.getParameterNameProvider()Ljavax/validation/ParameterNameProvider;
at org.hibernate.validator.internal.engine.ValidatorFactoryImpl.<init>(ValidatorFactoryImpl.java:119)
at org.hibernate.validator.HibernateValidator.buildValidatorFactory(HibernateValidator.java:45)
at org.hibernate.validator.internal.engine.ConfigurationImpl.buildValidatorFactory(ConfigurationImpl.java:217)
at javax.validation.Validation.buildDefaultValidatorFactory(Validation.java:111)
I found this question which seems quite similar to my problem. He describes his solution as
I had yet another bean validator jar in the class path. But not from
maven, so i didn't realize it. Removing that solved the problem.
I think my problem is the same. On http://hibernate.org/validator/documentation/getting-started/ it says:
This transitively pulls in the dependency to the Bean Validation API
(javax.validation:validation-api:1.1.0.Final)
That must be causing this issue, since reverting to an older version (4.3.1.Final) fixes the issue. Is there a way to force Hibernate to not pull in the Bean Validation API?
Edit: I've tried to exclude the javax-validation api:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.0.3.Final</version>
<exclusions>
<exclusion>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</exclusion>
</exclusions>
</dependency>
But it didn't seem to have any effect.
Try adding this dependency to your pom.xml
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
</dependency>
If not consider using hibernate-validator4.2.0.Final I have that one in my config and it is working fine.
For me, the 1.1.0.Final version javax.validation.validation-api had worked. Because, the javax.validation.spi.ConfigurationState interface of 1.1.0.Final has getParameterNameProvider method, which was absent in 1.0.0.GA.
I added the below dependency in pom.xml
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
<scope>test</scope>
</dependency>
I had the problem again. Thats how I've fixed that:
1-Exclude spring.validator from the 'web' dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</exclusion>
</exclusions>
</dependency>
2-After insert the dependecy with a previous version:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.3.Final</version>
</dependency>
in my case i just deleted the hibernate-validator and it worked .(i also had a combo of both validation api and hibernate-validator and tried everything) or you can go to your maven repository-->org and then delete the hibernate folder and rebuild your project again..
hope it helps..
I thought it would be useful to explain what is going on here.
Hibernate is calling ConfigurationState.getParameterNameProvider:
ValidatorFactoryImpl.java:
public ValidatorFactoryImpl(ConfigurationState configurationState) {
...
configurationState.getParameterNameProvider()
...
}
You can find the documentation of getParameterNameProvider:
getParameterNameProvider
ParameterNameProvider getParameterNameProvider()
Returns the parameter name provider for this configuration.
Returns:
parameter name provider instance or null if not defined
Since:
1.1
So what's the problem? The problem is that the method didn't always exist. It was added at some point in the future.
And the rule when creating interfaces is that they are set in concrete: you shall not change an interface ever. Instead the JavaX validator changed the ConfigurationState interface, and added a few new methods over the years.
The java validation code is passing the Hiberate an outdated ConfiguationState interface; one that doesn't implement the required interfaces.
You need to ensure that javax.validation.Validation.buildDefaultValidatorFactory is updated to to support version 1.1.
Removing this jar javax.validation:validation-api:1.1.0.Final solved my problem.
Make sure you have only one validation jar. If we have two jars then they may conflict resulting in error.
Go to the dependecies project and delete, hibernate.validator, and reinstall that in the most recent version. It has solved the problem for me.

Categories