问题
Hi Im creating a sample project for springMVc with hibernate with oracle as backend . I am stuck with one issue seems like some configuration issue but unable to identify whats wrong. Any guidance will be of great help.
org.hibernate.service.UnknownUnwrapTypeException: Cannot unwrap to requested type [javax.sql.DataSource]
The stacktrace:
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getTransactionManager' defined in common.configuaration.AppConfig: Invocation of init method failed; nested exception is org.hibernate.service.UnknownUnwrapTypeException: Cannot unwrap to requested type [javax.sql.DataSource]
Apr 05, 2018 5:42:38 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:oracle:thin:@host:port:SID]
Apr 05, 2018 5:42:38 AM org.springframework.web.context.ContextLoader initWebApplicationContext
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getTransactionManager' defined in common.configuaration.AppConfig: Invocation of init method failed; nested exception is org.hibernate.service.UnknownUnwrapTypeException: Cannot unwrap to requested type [javax.sql.DataSource]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:443)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:325)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4577)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5041)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1427)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1417)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:943)
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:839)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1427)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1417)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:943)
at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:258)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.StandardService.startInternal(StandardService.java:422)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:770)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.startup.Catalina.start(Catalina.java:682)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:353)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:493)
Caused by: org.hibernate.service.UnknownUnwrapTypeException: Cannot unwrap to requested type [javax.sql.DataSource]
at org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.unwrap(DriverManagerConnectionProviderImpl.java:217)
at org.springframework.orm.hibernate5.SessionFactoryUtils.getDataSource(SessionFactoryUtils.java:198)
at org.springframework.orm.hibernate5.HibernateTransactionManager.afterPropertiesSet(HibernateTransactionManager.java:353)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
find below my hibernate config
@Configuration
@EnableWebMvc
@EnableTransactionManagement
@ComponentScan(basePackages = "common.spring.service")
@ComponentScan(basePackages = "common.spring.dao")
public class AppConfig {
@Bean
public LocalSessionFactoryBean getSessionFactory() {
LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
Properties props = new Properties();
// Setting JDBC properties
props.put("hibernate.connection.driver_class", "oracle.jdbc.driver.OracleDriver");
props.put("hibernate.connection.url", "jdbc:oracle:thin:@host:port:SID");
props.put("hibernate.connection.username", "test");
props.put("hibernate.connection.password", "test123");
props.put("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
//props.put("hibernate.default_schema", "CCTRL1D");
// Setting Hibernate properties
props.put("hibernate.show_sql", true);
// props.put("hibernate.hbm2ddl.auto", "update");
// // Setting C3P0 properties
props.put("hibernate.c3p0.min_size","5");
props.put("hibernate.c3p0.max_size","20");
props.put("hibernate.c3p0.acquire_increment","1");
props.put("hibernate.c3p0.timeout","1000");
props.put("hibernate.c3p0.max_statements","150");
factoryBean.setHibernateProperties(props);
factoryBean.setPackagesToScan("common.spring.model");
return factoryBean;
}
@Bean
public HibernateTransactionManager getTransactionManager() {
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(getSessionFactory().getObject());
return transactionManager;
}
}
I am using below version of spring and hibernate
<springframework.version>4.3.10.RELEASE</springframework.version>
<hibernate.version>5.2.11.Final</hibernate.version>
回答1:
the error
org.hibernate.service.UnknownUnwrapTypeException: Cannot unwrap to requested type [javax.sql.DataSource
is pretty much self describing: Spring is the one to configure and set your database layer for you. For that it needs to have from you a javax.sql.DataSource already configured. You, instead are giving it through a group of properties values which are unusable.
try to modify AppConfig like this:
@Configuration
@EnableWebMvc
@EnableTransactionManagement
@ComponentScan(basePackages = "common.spring.service")
@ComponentScan(basePackages = "common.spring.dao")
public class AppConfig {
@Bean
public LocalSessionFactoryBean getSessionFactory() {
LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
Properties props = new Properties();
// Setting JDBC properties
// props.put("hibernate.connection.driver_class", "oracle.jdbc.driver.OracleDriver");
// props.put("hibernate.connection.url", "jdbc:oracle:thin:@host:port:SID");
//props.put("hibernate.connection.username", "test");
//props.put("hibernate.connection.password", "test123");
props.put("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
//props.put("hibernate.default_schema", "CCTRL1D");
// Setting Hibernate properties
props.put("hibernate.show_sql", true);
// props.put("hibernate.hbm2ddl.auto", "update");
// // Setting C3P0 properties
props.put("hibernate.c3p0.min_size","5");
props.put("hibernate.c3p0.max_size","20");
props.put("hibernate.c3p0.acquire_increment","1");
props.put("hibernate.c3p0.timeout","1000");
props.put("hibernate.c3p0.max_statements","150");
factoryBean.setHibernateProperties(props);
factoryBean.setPackagesToScan("common.spring.model");
return factoryBean;
}
@Bean
public HibernateTransactionManager getTransactionManager() {
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(getSessionFactory().getObject());
return transactionManager;
}
@Bean
public static DataSource getDataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("yourDriverClasName");
dataSource.setUrl("yourDbUrl");
dataSource.setUsername("yourDbUser");
dataSource.setPassword("yourDbPassowrd");
return dataSource;
}
}
来源:https://stackoverflow.com/questions/49669233/error-creating-transaction-manager-bean