I am getting an error in my application but I\'m not sure what it means. I posted some sample code bellow. This is the error I get:
> SEVERE: Exception sendin
This article suggests that you don't have a datasource defined in your hibernate config file.
I've exactly the same problem. KyelJmD suggests here that there is a missing connection pooling, and it seems he is right, but he didn't post solution.
So I've spent some time and finaly get it working. To solve this problem you should do two things:
add these lines into hibernate.cfg.xml file (it's possible that you don't need all of it):
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
Add this dependencies in pom.xml:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>4.1.9.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>4.1.9.Final</version>
</dependency>
Without maven dependency Hibernate just silently ignores C3P0 configuration.
With above configuration I dont't get anymore UnknownUnwrapTypeException exception, and it seems it works fine now.
My setup: Spring 3.2.1, Hibernate 4.1.9, Tomcat 7.0.35.
Dislaimer: as stated in this question, you should rather not use hibernate.cfg.xml file. Use dataSource
bean instead, because, for example, you can use it later for JdbcTemplate.
EDIT :
It's been a while, as I answered this question. It seemed correct, but I never like it, because it was kind of guess, which we should really avoid.
So, because I found this interresting and I was unable to find anything in Hibernate 4.1 docs, I tried to proof my and other commenters "guess".
Here is a step-by-step procedure, probably also useful for solving other weird issues in other libraries. Turns out this time it was really easy, what is going on.
UnknownUnwrapTypeException
) has been thrown (DriverManagerConnectionProviderImpl
) and navigate there (CTRL-N and type class name), click "Download sources".DriverManagerConnectionProviderImpl
(ALT+F7)DriverManagerConnectionProviderImpl
- it is a initiateService(...)
method in ConnectionProviderInitiator
, which - more or less - tries a couple ConnectionProvider
implementations, before it fallbacks to DriverManagerConnectionProviderImpl
, which in our case doesn't work and throws UnknownUnwrapTypeException
.From Hibernate's ConnectionProviderInitiator.java:
ConnectionProvider connectionProvider = null;
String providerClassName = getConfiguredConnectionProviderName( configurationValues );
if ( providerClassName != null ) {
connectionProvider = instantiateExplicitConnectionProvider( providerClassName, classLoaderService );
}
else if ( configurationValues.get( Environment.DATASOURCE ) != null ) {
connectionProvider = new DatasourceConnectionProviderImpl();
}
if ( connectionProvider == null ) {
if ( c3p0ConfigDefined( configurationValues ) && c3p0ProviderPresent( classLoaderService ) ) {
connectionProvider = instantiateExplicitConnectionProvider( C3P0_PROVIDER_CLASS_NAME,
classLoaderService
);
}
}
if ( connectionProvider == null ) {
if ( proxoolConfigDefined( configurationValues ) && proxoolProviderPresent( classLoaderService ) ) {
connectionProvider = instantiateExplicitConnectionProvider( PROXOOL_PROVIDER_CLASS_NAME,
classLoaderService
);
}
}
if ( connectionProvider == null ) {
if ( configurationValues.get( Environment.URL ) != null ) {
connectionProvider = new DriverManagerConnectionProviderImpl();
}
}
if ( connectionProvider == null ) {
LOG.noAppropriateConnectionProvider();
connectionProvider = new UserSuppliedConnectionProviderImpl();
}
The code roughly shows that:
Environment.DATASOURCE
(hibernate.connection.datasource) is set, then DatasourceConnectionProviderImpl
is used;Environment.URL
(hibernate.connection.url) is present, we use DriverManagerConnectionProviderImpl
.And that's it. This explains why if we add C3P0 as Maven Dependency or define DataSource (as Michael ako Tecourt says in comment below) solves the issue.
Another matter is why DriverManagerConnectionProviderImpl throws UnknownUnwrapTypeException - wheather it is a bug or not - anyway, I don't see any changes in code in Hibernate 4.2.0 and even 4.3.0.Beta.
try include spring and hibernate library to lib folder to your project. I hope these helps.
This article has detailed instruction on Spring 3.1 and Hibernate 4.1 integration:
http://blog.springsource.org/2012/04/06/migrating-to-spring-3-1-and-hibernate-4-1/
Just looking at the issue it's coming from this code:
if ( ConnectionProvider.class.equals( unwrapType ) ||
DriverManagerConnectionProviderImpl.class.isAssignableFrom( unwrapType ) ) {
return (T) this;
}
else {
throw new UnknownUnwrapTypeException( unwrapType );
}
where unwrapType is javax.sql.DataSource
.
Whilst I can't explain exactly what it is about your config that causes this I can suggest a fix.
Define a DriverManagerDataSource
bean in your spring application context and then provide this as a property to your LocalSessionFactoryBean
. Make sure you remove the datasource configuration from your Hibernate configuration file.