Using MySQL database for authentication in Ja-sig CAS - mysql

I'm trying to hook my Ja-sig CAS server (v3.5 running on Tomcat7) up to a MySQL database for user authentication. I basically have a table 'users' in the database storing username/password pairs that I want CAS to check against. However, I'm having difficulty even getting my current configuration to deploy.
This is an excerpt from pom.xml as it relates to database connectivity:
<dependency>
<groupId>org.jasig.cas</groupId>
<artifactId>cas-server-support-jdbc</artifactId>
<version>${cas.version}</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.22-bin</version>
<scope>provided</scope>
</dependency>
And here is where I try to setup the database connection in WEB-INF/deployerConfigContext.xml:
<bean
class="org.jasig.cas.adaptors.jdbc.SearchModeSearchDatabaseAuthenticationHandler">
<property name="tableUsers">
<value>users</value>
</property>
<property name="fieldUser">
<value>username</value>
</property>
<property name="fieldPassword">
<value>password</value>
</property>
<property name="passwordEncoder">
<bean
class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder">
<constructor-arg value="MD5" />
</bean>
</property>
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="datasource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/cas_db</value>
</property>
<property name="username">
<value>cas_server</value>
</property>
<property name="password">
<value>pass</value>
</property>
</bean>
It builds perfectly fine with Maven, but when I try to deploy it with Tomcat it doesn't work. I haven't been able to find anything particularly informative in any of the tomcat logs. I'm wondering if there might be a problem with 'commons-dbcp', since when I comment that out and use a simple authentication handler in deployerConfigContext.xml, I'm able to deploy.
There seems to be little/poor documentation of this from my current web research. If anyone has any good resources they could recommend as well, it would be greatly appreciated.

I finally found a trace of errors in the tomcat log localhost.YYYY-MM-DD.log. As it turns out, I needed to add commons-pool:
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
<scope>provided</scope>
</dependency>
which commons-dbcp is dependent upon. Installing this with Maven did away with the missing class exception I was getting.
My next problem was that I had mistakenly defined my datasource bean in the list of authenticationHandlers in deployerConfigContext.xml, which led to a type conversion exception. Moving the bean out of the list tag did the trick.

On one of the official guides for CAS + JDBC Authentication (https://wiki.jasig.org/display/CASUM/Using+JDBC+for+Authentication), they comment on that:
Note: It is recommended commons-dbcp 1.2.1 is used with MySQL instead of the newer version. I found that new version (1.2.2) will cause a Socket write error in MySQL, after your CAS is idle for more that 8 hours, which is the time that MySQL will clean up all idle connections.
Your problem might be related to the version of the commons-dbcp. In my case, I have a configuration similar to yours with the difference on the commons-dbcp version, I'm using 1.4 (no problems)

Related

LocalDate MySQL persistence comes back the date before

I have seen this question posted a few times on StackOverflow, and it has been with mostly Spring Boot apps with Spring Boot Configuration. I've followed all the steps, and I still have this issue. If I can't get this resolved soon, I will have to go back to the old java.util.Date in order to persist my data to the database ... at least it worked without issues. So, I have a Spring 5.1.2.Release app, with Hibernate 5.4.0.Final, the hibernate-java8 dependency, and the latest MySQL Connector 8.0.13.
I understand from previous posts and other articles on the Net that if the database is set for UTC, but the App is running in another timezone, in my case GMT-5 for EST, then this problem might pop up.
So, here are the technical details:
My MySQL connection looks like:
hibernate.connection.url=jdbc:mysql://localhost:3306/my_db?
serverTimezone=UTC&useLegacyDatetimeCode=false&useTimezone=true
The connection string, is used in the applicationContext.xml:
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>${hibernate.connection.driver.class}</value>
</property>
<property name="url">
<value>${hibernate.connection.url}</value>
</property>
<property name="username">
<value>${hibernate.connection.username}</value>
</property>
<property name="password">
<value>${hibernate.connection.password}</value>
</property>
</bean>
My Hibernate properties in my applicationContext.xml look like:
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">${hibernate.cglib.use_reflection_optimizer}</prop>
<prop key="useUnicode">true</prop>
<prop key="useLegacyDatetimeCode">false</prop>
<prop key="serverTimezone">UTC</prop>
<prop key="useTimezone">true</prop>
<prop key="hibernate.jdbc.time_zone">UTC</prop>
</props>
</property>
My Maven dependencies are:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-java8</artifactId>
<version>5.4.0.Final</version>
</dependency>
My Entity Code is simple:
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "transaction_id")
private long txId;
#Column(name = "transaction_date")
private LocalDate txDate;
I save my code to the database like this:
#Override
public TxEntity create(TxEntity txEntity)
{
this.sessionFactory.getCurrentSession().save(txEntity);
this.sessionFactory.getCurrentSession().flush();
this.sessionFactory.getCurrentSession().refresh(txEntity);
return txEntity;
}
The database has "transaction_date" as a DATE field in MySQL.
My Unit Test shows that I can create a new record, and I have set the date as follows:
LocalDate today = LocalDate.now();
I persist the record, and when I test:
assertEquals(today, myRecord.getTransactionDate());
this fails, because I get the previous date.
I thought that if I used the latest versions of Hibernate and the Hibernate-java8 I would be fine, but that's not the case. I thought if I pulled in the latest JPA2.2 that would work, but it's not. So, I think I am doing everything right, and I still have an issue.
So, if there is anything you need to see, let me know and I can add it. I think I am doing everything right, but if there is anything off I need to fix, please let me know. And of course, I'll keep looking at this myself.
If you don't want to touch your Java project or connection strings to solve this issue you can configure the MySQL timezone directly. On my computer the JVM timezone was correct (America/Sao_Paulo), so changing it to UTC in order to solve this conflict with MySQL was a no go.
The problem I had was just at the MySQL timezone configuration. Since I run MySQL on a container it got a wrong default timezone. The fix was to pass the --default-time-zone=America/Sao_Paulo to the MySQL container configuration, which fixed this issue. Now both MySQL and JVM got the same timezone.
For me, after some debugging for some time, I noticed Mysql timezone was UTC, but my default jvm timezone which can be seen by:
public class DefaultTimeZone {
public static void main(String[] args) {
System.out.println(java.util.TimeZone.getDefault().getID());
}
}
was America/Montreal so in updated my ide (IntellJ) settings to pass -Duser.timezone=UTC as a parameter when starting the application. (Of course there are so many other ways to do this, but in my case this was enough)

Gson and Jackson caused 'no handler method found for /restendpoint' in Spring MVC

I'm trying to add Swagger-UI in a Spring MVC Application. All requests and responses of org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter are serialized by gson 2.6.1 (see following code.)
When I tried to integrate Swagger, I had to add following dependencies:
<!-- json request -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.7</version>
</dependency>
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
The Integration of Swagger worked well, but some of my requests are broken.
I receive AbstractHandlerMethodMapping: 302 - Did not find handler method for /.... I guess Jackson and Gson are not compatible together in this way. When I remove the jackson.core dependency, everything works fine.
In the project it's absolutey necessary to use Gson, which we also set in the application context of spring.
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonConverter" />
</list>
</property>
</bean>
<bean id="gsonBuilder" class="com.google.gson.GsonBuilder" init-method="serializeNulls" />
<bean id="jsonConverter" class="org.springframework.http.converter.json.GsonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json" />
<property name="gson">
<bean class="com.google.gson.Gson" factory-bean="gsonBuilder"
factory-method="create" />
</property>
</bean>
My Question is, does someone have an idea why jackson and gson behaves like that in the same application? Or did I forgot or miss something, to avoid that jackson convert my requests/responses?
I hope I could clarify my problem in detail.
After some research I found out that Spring 4 automatically use Jackson when it's added to the classpath. In my *.xml config files I had <mvc:annotation-driven/>. It was causing a 2nd instance of RequestMappingHandlerAdapter with the default Jackson converters without GsonHttpMessageConverter. This was the reason, why all my RestEndpoints didn't work properly anymore, but Swagger did.
I migrated my project to use Jackson instead of Gson and I solved my problem.

Can't persist UTF-8 Symbols using MySQL, JPA, Hibernate

there a lot of simillar questions but non of them solve my problem.
I use JPA with hibernate and want to save UTF-8 characters.
My maven dependencies are:
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.6.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>3.6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>3.6.0</version>
</dependency>
So I use JPA 2.1 and QueryDSL 3.6.0.
My persistance.xml is:
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="thePersistenceUnit" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/newsDB?useUnicode=true&characterEncoding=UTF-8"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="root"/>
<!--Hibernate properties-->
<property name="hibernate.show_sql" value="false"/>
<property name="hibernate.format_sql" value="false"/>
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
</properties>
</persistence-unit>
</persistence>
Then I use XAMPP MySQL and Apache
And add
port= 3306
[client]
default-character-set = utf8
[mysqld]
init-connect='SET NAMES utf8'
character-set-server = utf8
collation-server = utf8_general_ci
[mysql]
default-character-set = utf8
My DB Collation is:
And my table:
When I turn on the logger I saw:
TRACE [main] (BasicBinder.java:81) - binding parameter [1] as [VARCHAR] - [Xelian]
TRACE [main] (BasicBinder.java:81) - binding parameter [2] as [VARCHAR] - [Описание]
TRACE [main] (BasicBinder.java:81) - binding parameter [3] as [CLOB] - [Content of the text .Чирипаха.]
TRACE [main] (BasicBinder.java:81) - binding parameter [4] as [BIGINT] - [112]
TRACE [main] (BasicBinder.java:81) - binding parameter [5] as [TIMESTAMP] - [Thu Jan 22 19:44:59 EET 2015]
TRACE [main] (BasicBinder.java:81) - binding parameter [6] as [VARCHAR] - [Title]
TRACE [main] (BasicBinder.java:81) - binding parameter [7] as [TIMESTAMP] - [Thu Jan 22 19:44:59 EET 2015]
TRACE [main] (BasicBinder.java:81) - binding parameter [8] as [BIGINT] - [0]
You can see that the values are strange Чирипаха.. And throught phpmyadmin I see:
But I when I edit it manually
You can see that my DB shows Cyrilic sumbols properlly. So I am wondering where is the problem.
So. I uninstall XAMPP an isntal MySQL Server on my machine. Then run MySQL command Line Client :
show variables like "%character%";show variables like "%collation%";
And everything seems to be OK. utf-8 every where. Then results in the DB was not encoded properlly, still strange symbols were there. When I edit them Cyrillic was inserted ok. SO I suspect the Hibernate persistance.xml. I edit it and put:
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="thePersistenceUnit" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/dnesdb?useUnicode=true&characterEncoding=UTF-8" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="root" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.connection.characterEncoding" value="utf8"/>
<property name="hibernate.connection.useUnicode" value="true"/>
<property name="hibernate.connection.charSet" value="UTF-8"/>
</properties>
</persistence-unit>
</persistence>
And now UTF-8 (Cyrillic) is persisted OK.
!!! And do not forget BEFORE creating the DB to set java property:
-Dfile.encoding=UTF-8
If the tables is created and after that you set -Dfile.encoding=UTF-8, the problem will stay. So drop the table set the java encoding property, then recreate table again.
I think you have solved this problem but my advice might be useful to other users.
I had a similar problem. I tried to use many of the recommendations from stackoverflow but to not avail. The problem was in the encoding of my Project in my IDE(Intellij IDEA). My Project has windows-1251 encoding and my database has utf8 encoding. You need to change your project encoding in according to your database. In Intellij : Settings->Editor->File Encodings->Project Encoding.

Adding mySql to eclipse project with maven tomcat plugin

I have a spring mvc project downloaded from the web and fully working. I'm using maven and maven tomcat plugin to manage dependencies and to run the webapp in the built-in tomcat. I'm trying to add mySql support in my project. Since i'm new to maven and maven tomcat plugin, I don't know hot to do this. Before i tried to add mysql, all was working and i was able to launch my web app simply executing a tomcat:run maven goal.
For now, when i execute tomcat:run i get a
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Here is what i've already done after some reading around the web:
I added dependencies for mysql driver (and Hibernate annotations too since i want to use it) in my pom.xml, and specified the dependency for tomcat plugin:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<mode>context</mode>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.9</version>
</dependency>
</dependencies>
</plugin>
You can also notice a tag to specify to use a context.xml file. But I don't know where to put this file. I readed it should be generated automatically in tomcat/conf, but it's not present. So i added it manually with this content:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/mkyongdb" auth="Container" type="javax.sql.DataSource"
maxActive="50" maxIdle="30" maxWait="10000"
username="root" password="password"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/mkyongdb"/>
</Context>
Then in web.xml, located in tomcat/conf i added:
<resource-ref>
<description>MySQL Datasource example</description>
<res-ref-name>jdbc/mkyongdb</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
I placed the same content in src/main/webapp/META-INF/context.xml and in src/main/webapp/WEB-INF/web.xml
With all these configuration, the error mentioned above doesn't appears. But if i try to use hibernate adding
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mkyongdb" />
<property name="username" value="root" />
<property name="password" value="password" />
</bean>
<bean
id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
<property name="dataSource" >
<ref bean="dataSource" />
</property>
<property name="hibernateProperties" >
<props>
<prop key="hibernate.hbm2ddl.auto" >create-drop</prop>
<prop key="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql" >true</prop>
</props>
</property>
<property name="annotatedClasses" >
<list>
<value>org.mose.grouporganizer.entity.AccelerometerFeatures</value>
</list>
</property>
</bean>
then i get the comunication link failure. What i'm missing?
If it's needed i can add the full stack trace.
If your application works fine and you want to use MySQL as your database then you should add MySQL driver in your pom.xml and change Hibernate configuration. That it.
First upgrade to last tomcat maven plugin which is now at Apache.
See http://tomcat.apache.org/maven-plugin-2.0/
Regarding the context use
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<configuration>
<contextFile>path to your context file</contextFile>
</configuration>
</plugin>

Hibernate, C3P0, Mysql -- Broken Pipe

MySQL seems to have an 8 hour time out on its connections. I'm running multiple WARs in Tomcat utilizing Hibernate for ORM. After 8 hours (i.e. overnight), I get broken pipes when it picks up an idle connection.
I've already traced through the code and made doubly sure I commit or rollback all transactions.
Here is my hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.bytecode.use_reflection_optimizer">false</property>
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="hibernate.current_session_context_class">thread</property>
<!--property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property-->
<property name="c3p0.min_size">3</property>
<property name="c3p0.max_size">5</property>
<property name="c3p0.timeout">1800</property>
<property name="c3p0.preferredTestQuery">SELECT 1</property>
<property name="c3p0.testConnectionOnCheckout">true</property>
<property name="c3p0.idle_test_period">100</property> <!-- seconds -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="cache.use_query_cache">false</property>
<property name="cache.use_minimal_puts">false</property>
<property name="max_fetch_depth">10</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- classes removed -->
</session-factory>
The parameter I thought would have fixed it was the c3p0.idle_test_period -- It defaults to 0. However, we still have the Broken Pipe issue after 8 hours of running. While there are multiple posts index via Google, none arrive at a satisfactory answer.
So it turns out I was missing a key line that enabled c3p0 (the c3p0 parameters I was tweaking were having no effect because Hibernate was using it's built in connection pool -- which it appropriately warns is not suitable for production). In hibernate 2.x, setting the hibernate.c3p0.max_size property enabled c3p0 connection pooling. However, in 3.x you must specify the following property --
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
Additionally, here are my final configuration parameters --
<property name="hibernate.c3p0.min_size">3</property>
<property name="hibernate.c3p0.max_size">5</property>
<property name="hibernate.c3p0.timeout">1800</property>
<property name="hibernate.c3p0.idle_test_period">100</property> <!-- seconds -->
It's rather unfortunate that both Hibernate and c3p0 have abysmal documentation in this regard.
There are two things going on here. You should read this article for more details, but the take-aways are:
You can adjust the MySQL wait_timeout setting to something larger than 8 hours, if desired.
The Hibernate settings should include "hibernate." before the "c3p0", e.g. hibernate.c3p0.idle_test_period instead of just c3p0.idle_test_period
This is a solution when you have a broken pipe because of combination of tomcat's wait_timeout=28800 sec(8h) and maxIdleTime=0 in c3p0:
I've changed the local tomcat wait_timeout via my.ini file to 120sec (2 min). And I placed the following:
maxIdleTime=100
idleConnectionTestPeriod=0 (same as default/as if it didn't exist)
other:
acquireIncrement=2
minPoolSize=2
maxPoolSize=5
maxIdleTimeExcessConnections=10
I had no problems with this setup.
I didn't need to use idleConnectionTestPeriod!
If tomcat's wait_timeout is 28800 sec, and maxIdleTime is 25200, it means that c3p0 will close the idle connection in 3600sec (1h) earlier, before tomcat throws a "broken pipe" exception. Isn't that right?!
As you can see I have no issues with providing only maxIdleTime.
Unfortunately, these:
maxIdleTime
idleConnectionTestPeriod
configuring_connection_testing
testConnectionOnCheckin
don't explain too much the corner cases.
And, btw, here is how to open the tomcat's my.ini file with Notepad++:
http://drupal.org/node/32715#comment-4907440
Cheers,
Despot
I've several problem -
- C3P0ConnectionProvider was not found
- I solve it by using the hibernate c3p0 version
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.5.6-Final</version>
</dependency>
<!-- c3p0 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>3.3.1.GA</version>
</dependency>
- I have that wait_timeout issue on mysql. First I set /etc/my.cnf wait_timeout=10
then I changed the Idle time out value to lower than the wait_timeout value which < 10
That solved my problem.
<property name="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider" /> <property name="hibernate.c3p0.acquire_increment" value="1" />
<property name="hibernate.c3p0.idle_test_period" value="28690"/>
<property name="hibernate.c3p0.timeout" value="1800" />
<property name="hibernate.c3p0.max_size" value="5" />
<property name="hibernate.c3p0.min_size" value="3" />
<property name="hibernate.c3p0.max_statement" value="50" />
<property name="hibernate.c3p0.preferredTestQuery" value="select 1;"/>
I was getting the same problem and it took time to figure out the solution.
I use Hibernate 4.0.1 and mysql 5.1(no spring framework) and I was facing the issue. First make sure that you configured the c3p0 jars properly which are essential.
I used these properties in hibernate.cfg.xml
<property name="hibernate.c3p0.validate">true</property>
<property name="hibernate.connection.provider_class">org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.preferredTestQuery">SELECT 1;</property>
<property name="hibernate.c3p0.testConnectionOnCheckout">true</property>
<property name="hibernate.c3p0.idle_test_period">10</property>
<property name="hibernate.c3p0.acquireRetryAttempts">5</property>
<property name="hibernate.c3p0.acquireRetryDelay">200</property>
<property name="hibernate.c3p0.timeout">40</property>
But it's of no use 'cause C3p0 was still taking the default properties not the properties which I set in hibernate.cfg.xml, You can check it in logs. So, I searched many websites for right solution and finally I came up with this. remove the C3p0 properties in cfg.xml and create c3p0-config.xml in the root path(along with cfg.xml) and set properties as follows.
<c3p0-config>
<default-config>
<property name="automaticTestTable">con_test</property>
<property name="checkoutTimeout">40</property>
<property name="idleConnectionTestPeriod">10</property>
<property name="initialPoolSize">10</property>
<property name="maxPoolSize">20</property>
<property name="minPoolSize">5</property>
<property name="maxStatements">50</property>
<property name="preferredTestQuery">SELECT 1;</property>
<property name="acquireRetryAttempts">5</property>
<property name="acquireRetryDelay">200</property>
<property name="maxIdleTime">30</property>
</default-config>
</c3p0-config>
but if you run, ORM takes the jdbc connection but not C3p0 connection pool 'cause we should add these properties in hibernate.cfg.xml
<property name="hibernate.c3p0.validate">true</property>
<property name="hibernate.connection.provider_class">org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider</property>
now everything works fine(At least it worked fine for me) and the issue is solved.
check the following for references.
http://www.mchange.com/projects/c3p0/index.html#configuring_connection_testing
https://community.jboss.org/wiki/HowToConfigureTheC3P0ConnectionPool
I hope this solves your problem.