title: Connect to an external Oracle database using JDBC
body: <p>I've setup a Spring project that access a remote Oracle database but only if I run this Spring project on my local Tomcat. If I deploy it to CF either as JavaWeb or Spring project I always get the same error message:</p>
  
  <blockquote>
    <p>root cause</p>
    
    <p>org.springframework.transaction.CannotCreateTransactionException:
    Could not open JPA EntityManager for transaction; nested exception is
    javax.persistence.PersistenceException:
    <strong>org.hibernate.exception.GenericJDBCException: Cannot open connection</strong>
      org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:3
    ....</p>
  </blockquote>
  
  <p>This is my config:</p>
  
  <pre><code>&lt;bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource"&gt;
      &lt;property name="driverType"&gt;
          &lt;value&gt;oracle.jdbc.driver.OracleDriver&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="URL"&gt;
          &lt;value&gt;${jdbc.url}&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="user"&gt;
          &lt;value&gt;${jdbc.username}&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="password"&gt;
          &lt;value&gt;${jdbc.password}&lt;/value&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" p:dataSource-ref="dataSource"&gt;
      &lt;property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/&gt;
      &lt;property name="jpaVendorAdapter"&gt;
          &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" p:showSql="true"
                p:generateDdl="true"&gt;
          &lt;/bean&gt;
      &lt;/property&gt;
      &lt;property name="jpaProperties"&gt;
          &lt;value&gt;
              hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
              hibernate.dialect=${hibernate.dialect}
              hibernate.hbm2ddl.auto=${hibernate.hbm2ddl.auto}
          &lt;/value&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>I've tried to use oracle.jdbc.OracleDriver instead of oracle.jdbc.driver.OracleDriver. I've tried to use <code>&lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"...</code>,  with corresponding property names. Also I've tried:</p>
  
  <pre><code>&lt;bean id="dataSource" class="org.springframework.jdbc.datasource.SingleConnectionDataSource"&gt;
      &lt;property name="driverClassName"&gt;
          &lt;value&gt;${database.driverClassName}&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="url"&gt;
          &lt;value&gt;${jdbc.url}&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="username"&gt;
          &lt;value&gt;${jdbc.username}&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="password"&gt;
          &lt;value&gt;${jdbc.password}&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="suppressClose" value="true"/&gt;
      &lt;property name="autoCommit" value="true"/&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>and </p>
  
  <pre><code>&lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
      &lt;property name="driverClassName"&gt;
          &lt;value&gt;${database.driverClassName}&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="url"&gt;
          &lt;value&gt;${jdbc.url}&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="username"&gt;
          &lt;value&gt;${jdbc.username}&lt;/value&gt;
      &lt;/property&gt;
      &lt;property name="password"&gt;
          &lt;value&gt;${jdbc.password}&lt;/value&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>Nothing worked, I get always the same error.</p>
  
  <p>I've tried to use HSQL with this config     </p>
  
  <p>
      
      
      
      
  </p>
  
  <p>and these jdbc.properties:</p>
  
  <pre><code># 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
  </code></pre>
  
  <p>...and <strong>it worked</strong> but when using these jdbc.properties:</p>
  
  <pre><code># 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
  </code></pre>
  
  <p>I get the error mentioned above.</p>