Hibernate 4 在 JBoss 7 上无法使用 c3p0
Hibernate 4 is not working c3p0 on JBoss 7
当我在 Jboss7 上部署我的 Spring4-Hibernate-4 应用程序时,它抛出错误并且部署失败。详情:
配置代码:
@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages = { "com.arg.utility.hibernate" })
@PropertySource(value = { "classpath:application.properties" })
public class HibernateConfigWS {
@Bean
public LocalSessionFactoryBean sessionFactoryWS() throws SQLException, NamingException {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSourceWS());
sessionFactory.setPackagesToScan(new String[] { "com.arg.utility.hibernate.model" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
@Lazy
public DataSource dataSourceWS() throws SQLException, NamingException {
InitialContext initContext = new InitialContext();
DataSource ds = null;
try{
ds = (DataSource)initContext.lookup(System.getProperty(UtilityConstants.JNDI_WS_DATASOURCE));
}catch(Exception e){
LOGGER.error("Datasource lookup failed :"+e.getStackTrace());
}
return ds;
}
@Bean
public HibernateTransactionManager transactionManagerWS(SessionFactory sessionFactoryWS) {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactoryWS);
return txManager;
}
//----------------------HIBERNATE COMMON PROPERTIES CONFIG---------------------//
/**
*
* @return properties object
*/
private Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
properties.put("hibernate.show_sql", "false");
properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
/*properties.put("hibernate.connection.provider_class", "org.hibernate.c3p0.internal.C3P0ConnectionProvider"); // commented
properties.put("hibernate.c3p0.min_size", "5"); // commented
properties.put("hibernate.c3p0.max_size", "20"); // commented
properties.put("hibernate.c3p0.timeout", "1800"); // commented
properties.put("hibernate.c3p0.max_statements", "50");*/ // commented
return properties;
}
}
JBoss 中的数据源条目 7 standalone.xml:
<datasource jndi-name="java:/WSDataSource" pool-name="WSDataSource" enabled="true" use-java-context="true">
<connection-url>jdbc:mysql://dshost:3306/ds_name</connection-url>
<connection-property name="zeroDateTimeBehavior">
convertToNull
</connection-property>
<driver>com.mysql</driver>
<pool>
<min-pool-size>0</min-pool-size>
<max-pool-size>10</max-pool-size>
<prefill>true</prefill>
</pool>
<security>
<user-name>abc</user-name>
<password>abc@123</password>
</security>
<validation>
<valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker"/>
<validate-on-match>false</validate-on-match>
<background-validation>true</background-validation>
<background-validation-millis>30000</background-validation-millis>
<exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter"/>
</validation>
</datasource>
部署应用程序时,出现以下错误:
<<< ServerService Thread Pool -- 68:2017/04/07 16:20:54.676:org.springframework.web.context.ContextLoader::::: Context initialization failed >>>
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManagerWS' defined in com.arg.hibernate.config.HibernateConfigWS: 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.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:866)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at io.undertow.servlet.core.ApplicationListeners.contextInitialized(ApplicationListeners.java:187)
at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:198)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:100)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.run(UndertowDeploymentService.java:82)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
at org.jboss.threads.JBossThread.run(JBossThread.java:320)
Caused by: org.hibernate.service.UnknownUnwrapTypeException: Cannot unwrap to requested type [javax.sql.DataSource]
at org.hibernate.engine.jdbc.connections.internal.UserSuppliedConnectionProviderImpl.unwrap(UserSuppliedConnectionProviderImpl.java:55)
at org.springframework.orm.hibernate4.SessionFactoryUtils.getDataSource(SessionFactoryUtils.java:106)
at org.springframework.orm.hibernate4.HibernateTransactionManager.afterPropertiesSet(HibernateTransactionManager.java:356)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
... 22 more
当我取消注释配置文件中的 c3p0 属性设置行时,应用程序已成功部署(但稍后在创建连接时抛出异常)。但是,我的要求是我不必使用 c3p0。所以我的查询:
- 是否可以在不使用 c3p0 的情况下 运行 在 Jboss 7 上休眠 4?
- 如果是,怎么样?是否需要任何其他配置?
如果您查看堆栈跟踪,它指的是 UserSuppliedConnectionProviderImpl
。这是抛出异常的地方。根据您的配置和 ConnectionProviderInitiator.initiateService
中的代码,我预计这将是 DatasourceConnectionProviderImpl
的一个实例。
AvailableSettings.DATASOURCE
由 LocalSessionFactoryBuilder.LocalSessionFactoryBuilder
构造函数设置,如果 DataSource
已提供,而您的构造函数已提供。您确定您的 JNDI 查找没有静默返回 null
吗?
当我在 Jboss7 上部署我的 Spring4-Hibernate-4 应用程序时,它抛出错误并且部署失败。详情:
配置代码:
@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages = { "com.arg.utility.hibernate" })
@PropertySource(value = { "classpath:application.properties" })
public class HibernateConfigWS {
@Bean
public LocalSessionFactoryBean sessionFactoryWS() throws SQLException, NamingException {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSourceWS());
sessionFactory.setPackagesToScan(new String[] { "com.arg.utility.hibernate.model" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
@Lazy
public DataSource dataSourceWS() throws SQLException, NamingException {
InitialContext initContext = new InitialContext();
DataSource ds = null;
try{
ds = (DataSource)initContext.lookup(System.getProperty(UtilityConstants.JNDI_WS_DATASOURCE));
}catch(Exception e){
LOGGER.error("Datasource lookup failed :"+e.getStackTrace());
}
return ds;
}
@Bean
public HibernateTransactionManager transactionManagerWS(SessionFactory sessionFactoryWS) {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactoryWS);
return txManager;
}
//----------------------HIBERNATE COMMON PROPERTIES CONFIG---------------------//
/**
*
* @return properties object
*/
private Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
properties.put("hibernate.show_sql", "false");
properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
/*properties.put("hibernate.connection.provider_class", "org.hibernate.c3p0.internal.C3P0ConnectionProvider"); // commented
properties.put("hibernate.c3p0.min_size", "5"); // commented
properties.put("hibernate.c3p0.max_size", "20"); // commented
properties.put("hibernate.c3p0.timeout", "1800"); // commented
properties.put("hibernate.c3p0.max_statements", "50");*/ // commented
return properties;
}
}
JBoss 中的数据源条目 7 standalone.xml:
<datasource jndi-name="java:/WSDataSource" pool-name="WSDataSource" enabled="true" use-java-context="true">
<connection-url>jdbc:mysql://dshost:3306/ds_name</connection-url>
<connection-property name="zeroDateTimeBehavior">
convertToNull
</connection-property>
<driver>com.mysql</driver>
<pool>
<min-pool-size>0</min-pool-size>
<max-pool-size>10</max-pool-size>
<prefill>true</prefill>
</pool>
<security>
<user-name>abc</user-name>
<password>abc@123</password>
</security>
<validation>
<valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker"/>
<validate-on-match>false</validate-on-match>
<background-validation>true</background-validation>
<background-validation-millis>30000</background-validation-millis>
<exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter"/>
</validation>
</datasource>
部署应用程序时,出现以下错误:
<<< ServerService Thread Pool -- 68:2017/04/07 16:20:54.676:org.springframework.web.context.ContextLoader::::: Context initialization failed >>>
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManagerWS' defined in com.arg.hibernate.config.HibernateConfigWS: 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.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:866)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at io.undertow.servlet.core.ApplicationListeners.contextInitialized(ApplicationListeners.java:187)
at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:198)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:100)
at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.run(UndertowDeploymentService.java:82)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
at org.jboss.threads.JBossThread.run(JBossThread.java:320)
Caused by: org.hibernate.service.UnknownUnwrapTypeException: Cannot unwrap to requested type [javax.sql.DataSource]
at org.hibernate.engine.jdbc.connections.internal.UserSuppliedConnectionProviderImpl.unwrap(UserSuppliedConnectionProviderImpl.java:55)
at org.springframework.orm.hibernate4.SessionFactoryUtils.getDataSource(SessionFactoryUtils.java:106)
at org.springframework.orm.hibernate4.HibernateTransactionManager.afterPropertiesSet(HibernateTransactionManager.java:356)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
... 22 more
当我取消注释配置文件中的 c3p0 属性设置行时,应用程序已成功部署(但稍后在创建连接时抛出异常)。但是,我的要求是我不必使用 c3p0。所以我的查询:
- 是否可以在不使用 c3p0 的情况下 运行 在 Jboss 7 上休眠 4?
- 如果是,怎么样?是否需要任何其他配置?
如果您查看堆栈跟踪,它指的是 UserSuppliedConnectionProviderImpl
。这是抛出异常的地方。根据您的配置和 ConnectionProviderInitiator.initiateService
中的代码,我预计这将是 DatasourceConnectionProviderImpl
的一个实例。
AvailableSettings.DATASOURCE
由 LocalSessionFactoryBuilder.LocalSessionFactoryBuilder
构造函数设置,如果 DataSource
已提供,而您的构造函数已提供。您确定您的 JNDI 查找没有静默返回 null
吗?