Skip to content

Commit

Permalink
update to match dev center style
Browse files Browse the repository at this point in the history
  • Loading branch information
jsimone committed Nov 30, 2011
1 parent 48ec692 commit d41f2e5
Showing 1 changed file with 49 additions and 13 deletions.
62 changes: 49 additions & 13 deletions README.md
@@ -1,7 +1,7 @@
Connecting to Relational Databases on Heroku with Java
======================================================

Applications on Heroku can use any back-end data storage system. You can either use a data storage system provided as a Heroku add-on or do your own thing. The data storage systems available as Heroku add-ons range from relational databases to NoSQL databases. Some of the relational databases that are available on Heroku are the shared Postgres database service from Heroku, the enterprise-grade Postgres database service from Heroku, various MySQL third party add-on providers, and the Oracle RDMS available from Amazon RDS. Every application you create is automatically provisioned a shared Postgres database. But you can easily add any other database service via Heroku add-ons.
Applications on Heroku can use any back-end data storage system. You can either use a data storage system provided as a Heroku add-on or do your own thing. The data storage systems available as Heroku add-ons range from relational databases to NoSQL databases. Some of the relational databases that are available on Heroku are the shared Postgres database service from Heroku, various MySQL third party add-on providers, and the Oracle RDMS available from Amazon RDS. Every application you create is automatically provisioned a shared Postgres database. But you can easily add any other database service via Heroku add-ons.

The relational database add-ons on Heroku provide the provisioned database connection information through an environment variable named `DATABASE_URL`. Since Heroku is a polyglot platform the format of the connection information is not specific to Java and will need to be parsed for use an a Java application that connects via JDBC. The format of the `DATABASE_URL` is:

Expand All @@ -15,16 +15,19 @@ Note: Play Framework has out-of-the-box support for the `DATABASE_URL` format.

You can see the `DATABASE_URL` provided to an application by running:

heroku config
:::term
$ heroku config
DATABASE_URL => postgres://foo:foo@heroku.com/hellodb

It is not recommended to copy this value into a static file since the environment may change the value. Instead the recommendation is for an application to read the `DATABASE_URL` environment variable and setup the database connections based on that information.
It is not recommended to copy this value into a static file since the environment may change the value. Instead an application should read the `DATABASE_URL` environment variable and setup the database connections based on that information.


Using the `DATABASE_URL` in Plain JDBC
------------------------------------

Here is a simple Java method that reads the `DATABASE_URL` environment variable and returns a `Connection`:
This simple Java method reads the `DATABASE_URL` environment variable and returns a `Connection`:

:::java
private static Connection getConnection() throws URISyntaxException, SQLException {
URI dbUri = new URI(System.getenv("DATABASE_URL"));

Expand All @@ -39,8 +42,9 @@ Here is a simple Java method that reads the `DATABASE_URL` environment variable
Using the `DATABASE_URL` in Spring with XML Configuration
-------------------------------------------------------

Here is a snippet of Spring XML configuration that will setup a `BasicDataSource` from the `DATABASE_URL` and can then be used with Hibernate, JPA, etc:
This snippet of Spring XML configuration will setup a `BasicDataSource` from the `DATABASE_URL` and can then be used with Hibernate, JPA, etc:

:::xml
<bean class="java.net.URI" id="dbUrl">
<constructor-arg value="#{systemEnvironment['DATABASE_URL']}"/>
</bean>
Expand All @@ -57,6 +61,7 @@ Using the `DATABASE_URL` in Spring with Java Configuration

Alternatively you can use Java for configuration of the `BasicDataSource` in Spring:

:::java
@Configuration
public class MainConfig {

Expand Down Expand Up @@ -122,17 +127,48 @@ For each command you should see a message like the following indicating that eve

To run on Heroku, first create a new application:

heroku create -s cedar
:::term
$ heroku create -s cedar
Creating stark-sword-398... done, stack is cedar
http://stark-sword-398.herokuapp.com/ | git@heroku.com:stark-sword-398.git
Git remote heroku added

Then deploy the application on Heroku:

git push heroku master

Now execute the examples on Heroku:

heroku run "sh devcenter-java-database-plain-jdbc/target/bin/main"
heroku run "sh devcenter-java-database-spring-xml/target/bin/main"
heroku run "sh devcenter-java-database-spring-java/target/bin/main"
:::term
$ git push heroku master
Counting objects: 70, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (21/21), done.
Writing objects: 100% (70/70), 8.71 KiB, done.
Total 70 (delta 14), reused 70 (delta 14)

-----> Heroku receiving push
-----> Java app detected
-----> Installing Maven 3.0.3..... done
-----> Installing settings.xml..... done
-----> executing /app/tmp/repo.git/.cache/.maven/bin/mvn -B -Duser.home=/tmp/build_2y7ju7daa9t04 -Dmaven.repo.local=/app/tmp/repo.git/.cache/.m2/repository -s /app/tmp/repo.git/.cache/.m2/settings.xml -DskipTests=true clean install
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] devcenter-java-database-plain-jdbc
[INFO] devcenter-java-database-spring-xml
[INFO] devcenter-java-database-spring-java
[INFO] devcenter-java-database
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building devcenter-java-database-plain-jdbc 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: http://s3pository.heroku.com/jvm/postgresql/postgresql/9.0-801.jdbc4/postgresql-9.0-801.jdbc4.pom
...

Now execute any of the examples on Heroku:

:::term
$ heroku run "sh devcenter-java-database-plain-jdbc/target/bin/main"
Running sh devcenter-java-database-plain-jdbc/target/bin/main attached to terminal... up, run.1
Read from DB: 2011-11-29 20:36:25.001468

Further Learning
----------------
Expand Down

0 comments on commit d41f2e5

Please sign in to comment.