我有一个在 Tomcat 和 Mysql 上使用 Spring 和 Hibernate 运行的 Web 应用程序。 我曾经在我的计算机上编写代码:一个基于 Windows 7 的环境:web 应用程序运行良好。
然后我尝试将它导出到远程 debian 服务器上,所以我安装了 tomcat 和 mysql,然后从我的 Windows mysql 数据库中导入一个转储。
即使我可以使用 mysql 控制台访问我的数据库,我的 webapp 也不能并给我这个错误:
20:35:03,020 DEBUG LogicalConnectionImpl:226 - Obtaining JDBC connection
20:35:03,021 DEBUG PoolableConnectionFactory:292 - Failed to validate a poolable connection
java.sql.SQLException: isValid() returned false
at org.apache.commons.dbcp2.PoolableConnection.validate(PoolableConnection.java:228)
at org.apache.commons.dbcp2.PoolableConnectionFactory.validateConnection(PoolableConnectionFactory.java:303)
at org.apache.commons.dbcp2.PoolableConnectionFactory.validateObject(PoolableConnectionFactory.java:288)
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:488)
at org.apache.commons.pool2.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:361)
at org.apache.commons.dbcp2.PoolingDataSource.getConnection(PoolingDataSource.java:119)
at org.apache.commons.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1413)
at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:139)
然后我搜索并找到了一些关于 "hibernate.hbm2ddl.auto" 的东西,所以我添加到我的 conf 中: prop.put("hibernate.hbm2ddl.auto", "验证");
现在:
20:53:24,701 ERROR ContextLoader:331 - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateMenuDao': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.meltdown.menu.infra.HibernateMenuDao.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class com.meltdown.config.ViewConfig: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.hibernate.SessionFactory com.meltdown.config.ViewConfig.sessionFactory()] threw exception; nested exception is org.hibernate.HibernateException: Missing table: BARS
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
看起来我的应用程序无法访问我的数据库,但我不明白为什么...
这是我的配置文件: 应用安全配置 @配置 @EnableWebSecurity 公共(public)类 AppSecurityConfig 扩展 WebSecurityConfigurerAdapter {
@Autowired
@Qualifier("userDetailsService")
UserDetailsService userDetailsService;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/bo/**").access("hasRole('ROLE_ADMIN')")
.antMatchers("/bo/bars**").access("hasRole('ROLE_SUPERADMIN')")
.and().formLogin();
}
@Override
public void configure(WebSecurity web) throws Exception {
web
.ignoring()
.antMatchers("/resources/**");
}
@Bean
public PasswordEncoder passwordEncoder(){
PasswordEncoder encoder = new BCryptPasswordEncoder();
return encoder;
}
}
MVC初始化器 公共(public)类 MVCInitializer 扩展了 AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { ViewConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
查看配置
@EnableWebMvc
@Configuration
@ComponentScan({ "com.meltdown.*" })
@EnableTransactionManagement
@Import({ AppSecurityConfig.class })
public class ViewConfig extends WebMvcConfigurerAdapter {
@Bean
public SessionFactory sessionFactory() {
LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(dataSource());
builder.scanPackages("com.meltdown").addProperties(getHibernateProperties());
return builder.buildSessionFactory();
}
private Properties getHibernateProperties() {
Properties prop = new Properties();
prop.put("hibernate.format_sql", "true");
prop.put("hibernate.show_sql", "true");
prop.put("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
prop.put("hibernate.hbm2ddl.auto", "validate");
return prop;
}
@Bean(name = "dataSource")
public BasicDataSource dataSource() {
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl("jdbc:mysql://localhost:3306/meltdown2");
ds.setUsername("root");
ds.setPassword("myXP4NYYKF8");
return ds;
}
//Create a transaction manager
@Bean
public HibernateTransactionManager txManager() {
return new HibernateTransactionManager(sessionFactory());
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/bo_theme/**");
}
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver
= new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/pages/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Bean
public MultipartResolver multipartResolver() {
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
resolver.setMaxUploadSize(1000000);
return resolver;
}
}
最佳答案
我不确定这是否是唯一的问题,但我在您的日志中注意到了这个错误:
缺少表:BARS
。
我知道 Windows 中的 mysql 表名默认情况下是不区分大小写的,而在 Linux/Unix 系统中它们是区分大小写的(我通过艰难的方式了解到,当我在 Windows 中开发的代码在我的 PC 上运行良好时,没有'部署在我们的 Linux 服务器上时无法工作)。
我假设您的代码引用表 BARS(所有大写字母),而数据库中定义的表是 Bars 或 bars,它适用于 Windows,但不适用于 Linux/Unix。
关于java - 将项目从 Windows 导出到 Debian 时出现 Mysql 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24770563/