我已经设置了一个访问远程 Oracle 数据库的 Spring 项目,但前提是我在本地 Tomcat 上运行这个 Spring 项目。如果我将它作为 JavaWeb 或 Spring 项目部署到 CF,我总是会收到相同的错误消息:
根本原因
org.springframework.transaction.CannotCreateTransactionException:无法为事务打开 JPA EntityManager;嵌套异常是 javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: 无法打开连接 org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:3 ....
这是我的配置:
<bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource">
<property name="driverType">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="URL">
<value>${jdbc.url}</value>
</property>
<property name="user">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" p:dataSource-ref="dataSource">
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" p:showSql="true"
p:generateDdl="true">
</bean>
</property>
<property name="jpaProperties">
<value>
hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
hibernate.dialect=${hibernate.dialect}
hibernate.hbm2ddl.auto=${hibernate.hbm2ddl.auto}
</value>
</property>
</bean>
我尝试使用 oracle.jdbc.OracleDriver 而不是 oracle.jdbc.driver.OracleDriver。我尝试使用<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"...
, 具有相应的属性名称。我也试过:
<bean id="dataSource" class="org.springframework.jdbc.datasource.SingleConnectionDataSource">
<property name="driverClassName">
<value>${database.driverClassName}</value>
</property>
<property name="url">
<value>${jdbc.url}</value>
</property>
<property name="username">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
<property name="suppressClose" value="true"/>
<property name="autoCommit" value="true"/>
</bean>
和
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>${database.driverClassName}</value>
</property>
<property name="url">
<value>${jdbc.url}</value>
</property>
<property name="username">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
</bean>
没有任何效果,我总是遇到同样的错误。
我尝试在此配置中使用 HSQL
和这些 jdbc.properties:
# HSQL
database.driverClassName = org.hsqldb.jdbcDriver
#jdbc.url = jdbc:hsqldb:file:target/zktodo2test.dat
jdbc.url = jdbc:hsqldb:file:zktodo2test.dat
jdbc.username = sa
jdbc.password =
hibernate.dialect = org.hibernate.dialect.HSQLDialect
hibernate.hbm2ddl.auto = update
...并且它有效,但是在使用这些 jdbc.properties 时:
# Oracle Credentials
database.driverClassName = oracle.jdbc.OracleDriver
jdbc.url = jdbc:oracle:thin:@dev.example.com:1521:dev
jdbc.username = epvin
jdbc.password = my_password
hibernate.dialect = org.hibernate.dialect.OracleDialect
hibernate.hbm2ddl.auto = update
我收到上面提到的错误。