Cannot unwrap to requested type Javax.Sql.Datasour

2020-08-01 05:27发布

问题:

I'm struggling to get hibernate working with Spring Boot. I'm getting the following exception:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'txManager' defined in com.xxx.MegLabApplication: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.hibernate5.HibernateTransactionManager]: Factory method 'txManager' threw exception; nested exception is org.hibernate.service.UnknownUnwrapTypeException: Cannot unwrap to requested type [javax.sql.DataSource]

The only thing I can find similar to this is the lack of c3p0 connection pooling, but I have the dependencies for c3p0 in my gradle build file and the properties are passed into hibernates configuration.

Can anyone advise on how I can fix this configuration?

Thanks

import java.util.Properties;

import org.apache.tomcat.jdbc.pool.DataSource;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder; 
import org.springframework.transaction.annotation.EnableTransactionManagement;

@EnableTransactionManagement
@SpringBootApplication
public class MegLabApplication
{

public static void main(String[] args)
{
    SpringApplication.run(MegLabApplication.class, args);
}

@Value("${spring.datasource.driverClassName}")
private String databaseDriverClassName;

@Value("${spring.datasource.url}")
private String datasourceUrl;

@Value("${spring.datasource.username}")
private String databaseUsername;

@Value("${spring.datasource.password}")
private String databasePassword;

@Bean
public SessionFactory sessionFactory()
{
    Configuration configuration = new Configuration();
    configuration.setProperties(getHibernateProperties());

    ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
            .applySettings(configuration.getProperties()).build();

    LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(dataSource());

    builder.scanPackages("com.xxx.models");

    return builder.buildSessionFactory(serviceRegistry);
}

private Properties getHibernateProperties()
{
    Properties prop = new Properties();
    prop.put("hibernate.connection.pool_size", "10");

    prop.put("hibernate.c3p0.min_size", "5");
    prop.put("hibernate.c3p0.max_size", "20");
    prop.put("hibernate.c3p0.timeout", "300");
    prop.put("hibernate.c3p0.max_statements", "50");
    prop.put("hibernate.c3p0.idle_test_period", "60");

    prop.put("hibernate.format_sql", "true");
    prop.put("hibernate.show_sql", "true");
    prop.put("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect");
    prop.put("hibernate.hbm2ddl.auto", "update");
    return prop;
}

@Bean(name = "dataSource")
public DataSource dataSource()
{
    DataSource ds = new DataSource();
    ds.setDriverClassName(databaseDriverClassName);
    ds.setUrl(datasourceUrl);
    ds.setUsername(databaseUsername);
    ds.setPassword(databasePassword);
    return ds;
}

@Bean
public HibernateTransactionManager txManager()
{
    return new HibernateTransactionManager(sessionFactory());
}
}

回答1:

Ok so I finally got this all working...

Spring boot uses hibernate 4.3.11. The HibernateTransactionManager class in my original config was using Hibernate5 so I changed the class version to 4. My C3P0 dependency was also version 5 so I downgraded it to 4.

I also had to add the connection details to the hibernate properties and explicitly tell it to use the C3P0 connection provider by specifying the class.

prop.put("hibernate.connection.provider_class", "org.hibernate.c3p0.internal.C3P0ConnectionProvider")

Hopefully this helps anyone coming across this later until Spring boot supports Hibernate 5.