我的spring webapp使用PostgreSQL,只有一些功能是数据库绑定的。我想在没有数据库连接的情况下部署应用程序。
如果没有数据库,dataSource bean会在启动时爆炸......并且所有后续请求都会因为bean 500s而失败,因为bean创建错误会停止应用程序初始化。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [database.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
etc.
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:877)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:805)
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:343)
etc.
Caused by: org.hibernate.exception.JDBCConnectionException: Unable to obtain JDBC Connection
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:115)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:95)
etc.
Caused by: org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:247)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:65)
at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:146)
at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:35)
etc.
数据源定义
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5432/sadb" />
<property name="username" value="sa" />
<property name="password" value="password" />
</bean>
当然这应该是可能的吗?我做了很多搜索,但无法找到相关内容。
答案 0 :(得分:0)
尝试使用嵌入式内存数据库,如h2或hsqldb
<jdbc:embedded-database id="dataSource" type="HSQL"/>
或
<jdbc:embedded-database id="dataSource" type="H2"/>
如果您不想使用jdbc名称空间
<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:mem:test"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
答案 1 :(得分:0)
我没有对此进行测试,但通过设置lazy-init =“true”来检查是否通过将数据库相关bean标记为懒惰加载来检查它是否有效。像这样:
<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource" lazy-init=”true”>
<property name="driverClass" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:mem:test"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
答案 2 :(得分:0)
我们在项目中面临同样的问题,我们通过实例化解决了问题 豆懒惰(lazy-init =“true”)如下:
<bean id="myProjectDataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
lazy-init="true">