我正在尝试使用hibernate和c3p0连接到mysql数据库,但是我遇到服务器超时错误(即使我将超时增加到几分钟)
我正在使用mysql,eclipse,maven,spring,tomcat 8.5,hibernate和c3p0。
这是我的.properties文件
# MySQL properties
mysql.driver=com.mysql.cj.jdbc.Driver
mysql.user=root
mysql.password=root
# Hibernate properties
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create
# C3P0 properties
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.acquire_increment=1
hibernate.c3p0.timeout=180
hibernate.c3p0.max_statements=150
配置
package com.bookshop.configuration;
import java.util.Properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import static org.hibernate.cfg.Environment.*;
@Configuration
@PropertySource("classpath:db.properties")
@EnableTransactionManagement
@ComponentScans(value = {
@ComponentScan("com.bookshop.model"),
@ComponentScan("com.bookshop.dao"),
@ComponentScan("com.bookshop.service")
})
public class AppConfig {
@Autowired
private Environment env;
@Bean
public LocalSessionFactoryBean getSessionFactory() {
LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
Properties prop = new Properties();
// Setting JDBC properties
prop.put(DRIVER, env.getProperty("mysql.driver"));
prop.put(URL, env.getProperty("mysql.url"));
prop.put(USER, env.getProperty("mysql.user"));
prop.put(PASS, env.getProperty("mysql.password"));
// Setting hibernate properties
prop.put(SHOW_SQL, env.getProperty("hibernate.show_sql"));
prop.put(HBM2DDL_AUTO, env.getProperty("hibernate.hbm2ddl.auto"));
// Setting C3P0 properties
prop.put(C3P0_MIN_SIZE, env.getProperty("hibernate.c3p0.min_size"));
prop.put(C3P0_MAX_SIZE, env.getProperty("hibernate.c3p0.max_size"));
prop.put(C3P0_ACQUIRE_INCREMENT, env.getProperty("hibernate.c3p0.acquire_increment"));
prop.put(C3P0_TIMEOUT, env.getProperty("hibernate.c3p0.timeout"));
prop.put(C3P0_MAX_STATEMENTS, env.getProperty("hibernate.c3p0.max_statements"));
factoryBean.setHibernateProperties(prop);
factoryBean.setPackagesToScan("com.bookshop.model");
return factoryBean;
}
@Bean
public HibernateTransactionManager getTransactionManager() {
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(getSessionFactory().getObject());
return transactionManager;
}
}
在这些日志行之后,服务器超时
...
INFO: Root WebApplicationContext: initialization started
jan 13, 2019 7:33:51 PM org.springframework.web.context.support.AnnotationConfigWebApplicationContext prepareRefresh
INFO: Refreshing Root WebApplicationContext: startup date [Sun Jan 13 19:33:51 MSK 2019]; root of context hierarchy
jan 13, 2019 7:33:51 PM org.springframework.web.context.support.AnnotationConfigWebApplicationContext loadBeanDefinitions
INFO: Registering annotated classes: [class com.bookshop.configuration.AppConfig]
jan 13, 2019 7:33:52 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.4.0.CR2}
jan 13, 2019 7:33:52 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
jan 13, 2019 7:33:52 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
jan 13, 2019 7:33:53 PM org.hibernate.c3p0.internal.C3P0ConnectionProvider configure
INFO: HHH010002: C3P0 using driver: com.mysql.cj.jdbc.Driver at URL: jdbc:mysql://localhost:3306/bookshop?autoReconnect=true&useSSL=false
jan 13, 2019 7:33:53 PM org.hibernate.c3p0.internal.C3P0ConnectionProvider configure
INFO: HHH10001001: Connection properties: {user=root, password=****}
jan 13, 2019 7:33:53 PM org.hibernate.c3p0.internal.C3P0ConnectionProvider configure
INFO: HHH10001003: Autocommit mode: false
jan 13, 2019 7:33:53 PM com.mchange.v2.log.MLog
INFO: MLog clients using java 1.4+ standard logging.
jan 13, 2019 7:33:53 PM com.mchange.v2.c3p0.C3P0Registry
INFO: Initializing c3p0-0.9.5.2 [built 08-December-2015 22:06:04 -0800; debug? true; trace: 10]
jan 13, 2019 7:33:53 PM org.hibernate.c3p0.internal.C3P0ConnectionProvider configure
INFO: HHH10001007: JDBC isolation level: <unknown>
jan 13, 2019 7:33:53 PM com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource
INFO: Initializing c3p0 pool...
com.mchange.v2.c3p0.PoolBackedDataSource@27499807
connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@69d301cd
[ acquireIncrement -> 1,
acquireRetryAttempts -> 30,
acquireRetryDelay -> 1000,
autoCommitOnClose -> false,
automaticTestTable -> null,
breakAfterAcquireFailure -> false,
checkoutTimeout -> 0,
connectionCustomizerClassName -> null,
connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester,
contextClassLoaderSource -> caller,
debugUnreturnedConnectionStackTraces -> false,
factoryClassLocation -> null,
forceIgnoreUnresolvedTransactions -> false,
forceSynchronousCheckins -> false,
identityToken -> 1hge0wga0jzt7ghyoftwg|30ba8455,
idleConnectionTestPeriod -> 0,
initialPoolSize -> 5,
maxAdministrativeTaskTime -> 0,
maxConnectionAge -> 0,
maxIdleTime -> 1800,
maxIdleTimeExcessConnections -> 0,
maxPoolSize -> 20,
maxStatements -> 150,
maxStatementsPerConnection -> 0,
minPoolSize -> 5,
nestedDataSource -> com.mchange.v2.c3p0.DriverManagerDataSource@2a1a6154
[ description -> null,
driverClass -> null,
factoryClassLocation -> null,
forceUseNamedDriverClass -> false,
identityToken -> 1hge0wga0jzt7ghyoftwg|15fd41d6,
jdbcUrl -> jdbc:mysql://localhost:3306/bookshop?autoReconnect=true&useSSL=false,
properties -> {user=******, password=******} ],
preferredTestQuery -> null,
privilegeSpawnedThreads -> false,
propertyCycle -> 0,
statementCacheNumDeferredCloseThreads -> 0,
testConnectionOnCheckin -> false,
testConnectionOnCheckout -> false,
unreturnedConnectionTimeout -> 0,
usesTraditionalReflectiveProxies -> false;
userOverrides: {} ],
dataSourceName -> null,
extensions -> {},
factoryClassLocation -> null,
identityToken -> 1hge0wga0jzt7ghyoftwg|6b84d329,
numHelperThreads -> 3 ]
(我正在学习,并且跟随我观看这些视频:https://www.youtube.com/watch?v=OyLF640v7WE)
谁能告诉我出什么问题了?
答案 0 :(得分:0)
最后,我解决了
在日志中写着“未找到hibernate.properties”,因此我将.properties文件从db.properties重命名为hibernate.properties,同时我也将其添加为属性文件的方言
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
并将其添加到AppConfig类中:
prop.put(DIALECT, env.getProperty("hibernate.dialect"));
现在一切正常。