Servlet调度程序的Spring + hibernate Servlet.init()引发异常 [英] Spring + hibernate Servlet.init() for servlet dispatcher threw exception
问题描述
我有一个示例项目,它声明了一个数据库表。该项目还通过REST公开了该表。该项目分为4个模块:
org.springframework.http.ResponseEntity
该应用发布到tomcat 8并按预期工作。 / p>
现在我试着在应用程序中添加另一个模型,因此我添加了一个新的.java到包含另一个带注释的模型/实体的模型子目录。
立即当我这样做时,我的应用程序不会再启动了。
我得到异常Servlet.init()为servlet调度器抛出异常
org.springframework.beans.factory.BeanCreationException:创建名称为'userDao'的bean时出错:注入自动装配依赖失败;
org.springframework.beans.factory.BeanCreationException:无法自动装载字段:private org.hibernate.SessionFactory sima.data.AbstractDao.sessionFactory;
org.springframework.beans.factory.BeanCreationException:在sima.config.HibernateConfiguration中定义名称为'sessionFactory'的bean时出错:init方法的调用失败;
org.hibernate.tool.schema.spi.SchemaManagementException:无法为架构管理目标打开JDBC连接
java.sql.SQLException:数据源已关闭
请注意,这个例外是针对我尚未修改的现有对象。我在models /子目录中添加了一个新对象,整个世界都爆炸了。
然后我尝试添加DAO和Controller,但它自然不会帮帮我。看起来我忘记了一些东西,但作为一个n00b,并没有解释人们给我的样品,我只是难住。可以你能帮我理解这一点吗?
你的DataSource是关闭的,因为后台发生错误。该错误可能是由您添加到项目中的新模型定义引起的。既然你提到你正在使用hibernate,但没有太多经验,我相信发生了什么:
- 你添加了一个新模型(引用表)
- 引用的表不存在(或只是未正确引用)
- 应用程序启动时,模式验证失败
- 底层数据库连接已关闭
为了解决问题,我会暂时建议至少增加调试级别(此时您肯定会看到关闭连接背后的原因 - 真正的错误)。您应该考虑切换hibernate调试日志记录来确保。
First and foremost: I'm a total n00b at this and am really trying to understand this, but these particular exceptions don't help me too much. So maybe I just need a pointer to appropriate documentation or maybe not, but this is killing me now for a couple of days and I'm no closer to anything resembling a solution.
I got a sample project which declared one DB table. That project also exposed this table via REST. The project is broken down into 4 modules:
- config: contains various configuration items, such as hibernate configuration, etc. All configuration is in .java files, not .xml
- model: contains DB model declarations / entities (also .java annotated)
- data: contains DAOs and their implementations for each model (one so far). Contains AbstractDao (simple sessionFactory wrapper / persistence implementor), UserDao (interface for rest methods) and UserDaoImpl (implementation for users model)
- web: controllers publishing DAOs to REST via
org.springframework.http.ResponseEntity
The app publishes to tomcat 8 and works as expected.
Now I tried to add another model into the app, so I added a new .java to models subdirectory containing another annotated model / entity.
Immediately when I do that, my app won't start any more.
I get exception Servlet.init() for servlet dispatcher threw exception
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao': Injection of autowired dependencies failed;
org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory sima.data.AbstractDao.sessionFactory;
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in sima.config.HibernateConfiguration: Invocation of init method failed;
org.hibernate.tool.schema.spi.SchemaManagementException: Unable to open JDBC connection for schema management target
java.sql.SQLException: Data source is closed
please note that this exception is for the existing object which I haven't modified. I added a new object into models/ subdirectory and the entire world exploded around me.
Then I tried to add the DAOs and Controller as well, but it naturally didn't help. It would seem I'm forgetting something, but being a n00b and having had no explanation on the subject from the people giving me the sample I'm just stumped.
Can you help me understand this?
Your DataSource is closed because of an error happening in the background. That error is likely caused by the new model definition you added to your project. Since you mentioned that you are using hibernate but not having too much experience with it yet, I believe what happened is that:
- you added a new model (referencing a table)
- the referenced table does not exist (or just not referenced correctly)
- schema validation failed when the application started up
- the underlying DB connection got closed
In order to resolve the problem I would suggest temporarily increasing the at least debug level (at this point you would surely see the reason behind the closed connection - the real error). You should probably consider switching on hibernate debug logging just to be sure.
这篇关于Servlet调度程序的Spring + hibernate Servlet.init()引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!