How to configure "Kitchensink Example" to use postgresql database?

The OpenShift forums have been retired.
You can still read and search them, but for help, please post a question on Stack Overflow.

Does anyone know what I need to add/edit to wire the "Kitchensink Example" app to use postgresql instead of default H2 that it comes with? I have added "PostgreSQL Database 8.4" as a cartridge, but not sure what other settings I need to provide in 'persistance.xml' and in 'kitchensink-quickstart-ds.xml' files.
Here are the contents of kitchensink-quickstart-ds.xml ...

 
 <datasource jndi-name="java:jboss/datasources/KitchensinkQuickstartDS"
      pool-name="kitchensink-quickstart" enabled="true"
      use-java-context="true">
      <connection-url>postgresql://127.2.210.1:5432/myApp</connection-url>
 
      <driver-class>org.postgresql.Driver</driver-class>
      <security>
         <user-name>admin</user-name>
         <password>myPassword</password>
      </security>
   </datasource>

and the persistance.xml ...

   <persistence-unit name="primary">
      <!-- If you are running in a production environment, add a managed 
         data source, this example data source is just for development and testing! -->
      <!-- The datasource is deployed as WEB-INF/kitchensink-quickstart-ds.xml, you
         can find it in the source at src/main/webapp/WEB-INF/kitchensink-quickstart-ds.xml -->
      <jta-data-source>java:jboss/datasources/KitchensinkQuickstartDS</jta-data-source>
      <properties>
         <!-- Properties for Hibernate -->
 
         <property name="hibernate.show_sql" value="true" />
      </properties>
   </persistence-unit>

I have spent two days trying different options but they all break the app. =(

Hi maratkurbanov;

The .openshift/config/standalone.xml defines the PostgreSQLDS datasource for you.  All you should need to do is to update persistence.xml to use:  java:jboss/datasources/PostgreSQLDS

So from:
      <jta-data-source>java:jboss/datasources/KitchensinkQuickstartDS</jta-data-source>
To:
      <jta-data-source>java:jboss/datasources/PostgreSQLDS</jta-data-source>

Hope that helps;
~Nam

Hi Nam!

That worked! Thank you so much! =)

Marat

Hi, I'm trying to do exactly the same, unfortunately, it doesn't work for me. My persistence.xml file looks like this:

 <?xml version="1.0" encoding="UTF-8"?>
 <persistence version="2.0"
  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_2_0.xsd">
  <persistence-unit name="primary">
     <jta-data-source>java:jboss/datasources/PostgreSQLDS</jta-data-source> 
     <properties>
        <property name="hibernate.show_sql" value="true" />
     </properties>
  </persistence-unit>
 </persistence>

The standalone.xml in git contains this

               <datasource jndi-name="java:jboss/datasources/PostgreSQLDS" enabled="${postgresql.enabled}" use-java-context="true" pool-name="PostgreSQLDS" use-ccm="true">
                   <connection-url>jdbc:postgresql://${env.OPENSHIFT_DB_HOST}:${env.OPENSHIFT_DB_PORT}/${env.OPENSHIFT_GEAR_NAME}</connection-url>
                   <driver>postgresql</driver>
                   <security>
                     <user-name>${env.OPENSHIFT_DB_USERNAME}</user-name>
                     <password>${env.OPENSHIFT_DB_PASSWORD}</password>
                   </security>
               </datasource>

The jbossas-7/standalone/configuration/standalone.xml file in the cloud looks like this

               <datasource jndi-name="java:jboss/datasources/PostgreSQLDS" enabled="true" use-java-context="true" pool-name="PostgreSQLDS" use-ccm="true">
                   <connection-url>jdbc:postgresql://${env.OPENSHIFT_DB_HOST}:${env.OPENSHIFT_DB_PORT}/invite</connection-url>
                   <driver>postgresql</driver>
                   <security>
                     <user-name>${env.OPENSHIFT_DB_USERNAME}</user-name>
                     <password>${env.OPENSHIFT_DB_PASSWORD}</password>
                   </security>
               </datasource>

Yet I get the following errors in the log when I push the project

]) - failure description: "JBAS014802: Cannot resolve expression 'expression \"jdbc:postgresql://${env.OPENSHIFT_DB_HOST}:${env.OPENSHIFT_DB_PORT}/invite\"' -- java.lang.IllegalStateException: Failed to resolve 
expression: ${env.OPENSHIFT_DB_HOST}"

and

2012/12/17 02:10:27,951 INFO  [org.jboss.weld.deployer] (MSC service thread 1-1) JBAS016005: Starting Services for CDI deployment: ROOT.war 
2012/12/17 02:10:30,227 INFO  [org.jboss.weld.Version] (MSC service thread 1-1) WELD-000900 1.1.5 (AS71) 
2012/12/17 02:10:31,433 ERROR [org.jboss.as] (MSC service thread 1-3) JBAS015875: JBoss AS 7.1.0.Final "Thunder" started (with errors) in 102201ms - Started  250 of 395 services (29 services failed or missing dependencies, 113 services are passive or on-demand)
2012/12/17 02:10:31,636 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) JBAS015870: Deploy of deployment "ROOT.war" was rolled back with failure  message {"JBAS014771: Services with missing/unavailable dependencies" =>  ["jboss.persistenceunit.\"ROOT.war#primary\"jboss.naming.context.java.jboss.datasources.PostgreSQLDSMissing[jboss.persistenceunit.\"ROOT.war#primary\"jbos s.naming.context.java.jboss.datasources.PostgreSQLDS]"]}
2012/12/17 02:10:31,919 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-4) JBAS015877: Stopped deployment ROOT.war in 279ms
2012/12/17 02:10:31,923 INFO  [org.jboss.as.controller] (DeploymentScanner-threads - 2) JBAS014774: Service status report
JBAS014775:    New missing/unsatisfied dependencies:
     service jboss.naming.context.java.jboss.datasources.PostgreSQLDS (missing) dependents: [service jboss.persistenceunit."ROOT.war#primary"] 
2012/12/17 02:10:31,927 ERROR [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 1) {"JBAS014653: Composite operation failed and was  rolled back. Steps that failed:" => {"Operation step-2" => {"JBAS014771: Services with missing/unavailable dependencies" =>  ["jboss.persistenceunit.\"ROOT.war#primary\"jboss.naming.context.java.jboss.datasources.PostgreSQLDSMissing[jboss.persistenceunit.\"ROOT.war#primary\"jbos s.naming.context.java.jboss.datasources.PostgreSQLDS]"]}}}


Hi @konnew;

The standalone.xml file you're using looks old. Where did you get it from? - cause the kitchensink quickstart (https://github.com/openshift/kitchensink-example) doesn't come with one as it assumes to use the latest and greatest from a fresh new app. The way you can tell is with the database env variables

It should be safe to go ahead and change those env variables (ssh onto the gear and run 'env | grep OPENSHIFT_POSTGRESQL':
OPENSHIFT_POSTGRESQL_DB_PASSWORD=
OPENSHIFT_POSTGRESQL_DB_HOST=
OPENSHIFT_POSTGRESQL_DB_GEAR_DNS=
OPENSHIFT_POSTGRESQL_DB_USERNAME=
OPENSHIFT_POSTGRESQL_DB_GEAR_UUID=
OPENSHIFT_POSTGRESQL_DB_URL=
OPENSHIFT_POSTGRESQL_DB_PORT=
(values omitted to protect the innocent)

If the standalone.xml used is too old however, it's probably safer to create a new app and pull that one over, or pull the kitchensink sample to the new app.

HTHs;
~Nam

The standalone.xml was from the kitchensink example I had downloaded a couple of months ago. Replacing it with the one from the current kitchen sink example solved the problem. Thanks.

Awesome! Thanks for the confirmation!

I've changed my persistentence.xml to java:jboss/datasources/PostgreSQLDS

     <property name="hibernate.show_sql" value="true" />
  </property></properties>

However I am getting error JBAS014775: New missing/unsatisfied dependencies: service jboss.naming.context.java.jboss.datasources.PostgreSQLDS (missing) dependents: [service jboss.persistenceunit."ft.war#primary"]

12:36:49,910 ERROR [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 1) {"JBAS014653: Composite operation failed and was rolled back. Steps that failed:" => {"Operation step-2" => {"JBAS014771: Services with missing/unavailable dependencies" => ["jboss.persistenceunit.\"ft.war#primary\"jboss.naming.context.java.jboss.datasources.PostgreSQLDSMissing[jboss.persistenceunit.\"ft.war#primary\"jboss.naming.context.java.jboss.datasources.PostgreSQLDS]"]}}}

Can somebody help me?

I noticed that you're not using the Kitchensink sample. Can you check your local repo/.openshift/config/standalone.xml for this configuration:

                <datasource jndi-name="java:jboss/datasources/PostgreSQLDS" enabled="${postgresql.enabled}" use-java-context="true" pool-name="PostgreSQLDS" use-ccm="true">
                    <connection-url>jdbc:postgresql://${env.OPENSHIFT_POSTGRESQL_DB_HOST}:${env.OPENSHIFT_POSTGRESQL_DB_PORT}/${env.OPENSHIFT_APP_NAME}</connection-url>
                    <driver>postgresql</driver>
                    <security>
                      <user-name>${env.OPENSHIFT_POSTGRESQL_DB_USERNAME}</user-name>
                      <password>${env.OPENSHIFT_POSTGRESQL_DB_PASSWORD}</password>
                    </security>
                    <validation>
                        <check-valid-connection-sql>SELECT 1</check-valid-connection-sql>
                        <background-validation>true</background-validation>
                        </validation>
                        <pool>
                                                <flush-strategy>IdleConnections</flush-strategy>
                                        </pool>
                </datasource>

The default repo should have those configs in standalone.xml

Also make sure you have the postgresql db added to your application. I also noticed that if you added postgresql after the fact, you'll need to restart your JBoss app for the enabled="${postgresql.enabled}" flag and env variables to be read in properly.

Hello

I' am new to Openshift . I want create JavaEE projekt with postgresql . I have added postgresql cartridge to my projekt. I got username and password for postgresql. I want to create persistence.xml. My question: datasource jndi-name="java:jboss/datasources/PostgreSQLDS" is default ? And enough to use JTA in my persistence.xml? Or I have to define jta? And where ? I have'nt got .openshift/config/standalone.xml .

Joe

You should probably try cloning the kitchen-sink example and try a few things with it, then you should be able to copy configurations over from it to your project and use them.

https://github.com/openshift/kitchensink-example/