BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News VMware's CloudFoundry Service Gains Support for PostgreSQL

VMware's CloudFoundry Service Gains Support for PostgreSQL

This item in japanese

Bookmarks

Earlier this year VMware released CloudFoundry, an open-source PAAS solution, with initial support for several services such as MongoDB, MySQL, and Redis. Lately it has added PostgreSQL and RabbitMQ to the list of cloud services that applications can depend on, as well as offering a Micro edition of the cloud that can run on a single workstation.

PostgreSQL is an interesting addition since it is a full featured traditional database with a different audience than MySQL or MongoDB users. To accommodate PostgreSQL, cloudfoundry does not use a vanilla version but instead a customized one based on vFabric as explained in the official blog post.

The same post gives an example of using PostgreSQL in a Java project built with Spring Roo. Here we illustrate an alternative approach where the Java application is a Spring based WAR utilizing JPA.

Assume that your application has a Spring context setup as below:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
	<property name="persistenceUnitName" value="myJpaManager" />
	<property name="dataSource" ref="dataSource"/>
</bean>

<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
	<property name="driverClassName" value="org.postgresql.Driver" />
	<property name="url" value="jdbc:postgresql://localhost/postgres" />
	<property name="username" value="postgres" />
	<property name="password" value="postgres" />
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
	<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />
<context:annotation-config />

and a META-INF/persistence.xml like this

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
             version="1.0">
	<persistence-unit name="myJpaManager" transaction-type="RESOURCE_LOCAL">
		<provider>org.hibernate.ejb.HibernatePersistence</provider>
	<properties>
		<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
		<property name="hibernate.hbm2ddl.auto" value="update" />
	</properties> 
	</persistence-unit>
</persistence>

This application can be uploaded to CloudFoundry completely unchanged thanks to the auto-configuration feature of CloudFoundry.

PostgreSQL (as well as MySQL and the rest of supported services) takes advantage of the auto-configuration feature offered by Cloudfoundry. Under certain conditions a local Java Application that uses PostgreSQL can be uploaded to CloudFoundry with zero code changes.

Auto-configuration employs the Spring Core container and takes places when both of the following are true

  1. There may exactly be only one service of a given service type. For example, you may bind only one relational database service (MySQL or PostgreSQL) to an application.
  2. There may exactly be only one bean of the matching type. For example, you may have only one DataSource bean in the application context.

If these requirements hold then Cloudfoundry will automatically intercept your local datasource and use the Cloudfoundry service instead of what is set in the Spring context.

The only thing left to do is to answer positively when vmc asks for service binding when the application is uploaded to the cloud.

For more complex applications, Spring 3.1 is expected to add a special cloud namespace and runtime profile support.

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

  • How to create tables on PostgreSQL

    by jorge middleton,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    How can I do if I want to create my schema and populate my tables. I know that I can use VMware vFabric Postgres, but I not sure if it's opensource and where I can download it?

    Thanks.

  • Re: How to create tables on PostgreSQL

    by Li Xin,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    There're three ways for you to create schema and populate data.

    1. You can create your schema and populate data into tables within your application. On start up, your application can check whether your tables exist or not, if not, run a few DDL scripts to create them and DML scripts to populate data.

    2. Depending on what framework you use for your application, you can leverage what the framework provides to do it for you. For example, with Spring framework, you can specify sql script files to initialize datasource, see static.springsource.org/spring/docs/current/spr...

    3. Cloud Foundry is going to provide a new feature that allows database native tool to connect to the database running inside Cloud Foundry. When it's available, you can use psql or pgAdmin III to connect to vPostgres. Stay tuned.

  • Re: How to create tables on PostgreSQL

    by Li Xin,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    From 17 Nov., Cloud Foundry allows developer to open a tunnel to any Cloud Foundry data service via a local port with a preview release of Cloud Foundry command line tool (‘VMC’), PostgreSQL is included as well.

    For details, please visit their blog page
    blog.cloudfoundry.com/post/12928974099/now-you-...

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT