1 Reply Latest reply: Jun 4, 2013 10:54 AM by Michael OBrien RSS

    Database access from Oracle Java Cloud Service application

    1008615
      I have registered for Oracle Java Cloud Service for trial period. I want to deploy a Spring MVC 3.2 and Hibernate 4.0 web application.

      I created a table in the Database Cloud. I am not sure how do I access the Database Cloud Service from my application.

      Need your guidance/pointers/references that can help me establish connection from my application to the database.

      Thanks,
      Ujjwal
        • 1. Re: Database access from Oracle Java Cloud Service application
          Michael OBrien
          Hi,
          Use JPA - see the visitors example, it uses @PersistenceUnit injection with previously weaved .class entity files.
          You can use application managed EMF's and EntityManagers with code like the following - which is not preferable to using @PersistenceContext injection on an @Stateless session EJB but...

                    EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPAServletPU");
                    EntityManager em = emf.createEntityManager();
                    EntityTransaction transaction = null;
                    try {
                         transaction = em.getTransaction();
                         transaction.begin();
                         Visitor entity = new Visitor();
                         entity.setName(name);
                         entity.setNum(num);
                         em.persist(entity);
                         transaction.commit();
                         System.out.println("Committing: " + entity);
                    
                    } catch (Exception e) {
                         e.printStackTrace();
                    } finally {
                         em.close();
                         emf.close();
                    }


          Use a persistence.xml like the following

          <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="JPAServletPU" transaction-type="JTA">
          <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
               <!--https://cloud.oracle.com/mycloud/f?p=5300:1000:259334829915901-->
          <jta-data-source>database</jta-data-source>
          <class>com.vision.cloud.jpa.Visitor</class>
          <exclude-unlisted-classes>false</exclude-unlisted-classes>
          <properties>
               <property name="eclipselink.target-database" value="Oracle10g" />
          <property name="eclipselink.ddl-generation" value="create-tables"/>
          <!-- property name="eclipselink.ddl-generation" value="drop-and-create-tables"/-->
          </properties>
          </persistence-unit>
          </persistence>

          Check out a tutorial on WebLogic JPA ORM usage here to get started as well.
          http://wiki.eclipse.org/EclipseLink/Examples/JPA/WebLogic_Web_Tutorial