简体   繁体   中英

How SAP HANA Cloud support multiple databases?

I have a scenario where i need to deploy my vaadin application(working fine with multiple databases(2 Persistence units) where runtime is tomcat 7) in SAP HANA Cloud platform(Target runtime is JAVA web).

My earlier database connection was with MYSQL by using eclipselink in below way

private EntityManager getEntityManager(String dbName) {
        EntityManagerFactory emf = null;
        Map<String, String> properties = new HashMap<String, String>();
        if (dbName.toLowerCase().contains("master"))
            properties.put(
                    PersistenceUnitProperties.ECLIPSELINK_PERSISTENCE_XML,
                    "/META-INF/persistence.xml");
        else
            properties.put(
                    PersistenceUnitProperties.ECLIPSELINK_PERSISTENCE_XML,
                    "/META-INF/persistenceYear.xml");// +dbName.substring(dbName.length()-4)+".xml");
        properties.put("javax.persistence.jdbc.driver", getDriver());
        properties
                .put("javax.persistence.jdbc.url",
                        getUrl()
                                + dbName
                                + "?zeroDateTimeBehavior=convertToNull&amp;useUnicode=yes&amp;characterEncoding=utf-8;charset=utf8;characterSetResults=utf8");// &amp;characterSetResults=utf8");
        // properties.put("javax.persistence.jdbc.url",
        // getUrl()+dbName+"?useUnicode=yes&amp;characterEncoding=UTF-8;characterSetResults=utf8");
        properties.put("javax.persistence.jdbc.user", getUserName());
        properties.put("javax.persistence.jdbc.password", getPassword());
        properties.put(PersistenceUnitProperties.TABLE_CREATION_SUFFIX,
                "engine=InnoDB DEFAULT CHARSET=utf8");

        properties.put(PersistenceUnitProperties.DDL_GENERATION,
                PersistenceUnitProperties.CREATE_OR_EXTEND);
        properties.put(PersistenceUnitProperties.DDL_GENERATION_MODE,
                PersistenceUnitProperties.DDL_DATABASE_GENERATION);
        // properties.put(PersistenceUnitProperties.CREATE_JDBC_DDL_FILE,"create.sql");
        // properties.put(PersistenceUnitProperties.APP_LOCATION,
        // ServerFolderPathConstants.PROPERTYFILES);

        // Configure Session Customizer which will pipe sql file to db before
        // DDL Generation runs
        // properties.put(PersistenceUnitProperties.SESSION_CUSTOMIZER,
        // "in.calico.finbook.config.common.ImportSQL");
        // properties.put("import.sql.file",ServerFolderPathConstants.PROPERTYFILES+"create.sql");
        try {
            /*
             * emf = Persistence.createEntityManagerFactory("finbook",
             * properties);
             */
            emf = Persistence.createEntityManagerFactory(getUnit(), properties);
        } catch (Exception e) {
            // System.out.println(e.getMessage());
            e.printStackTrace();
        }
        return emf.createEntityManager();
    }

Above method helps in connecting to database(MYSQL) and returns entity Manager.

The same methodology i need in my application which is gonna deploy in SAP HANA CLOUD. I want to know how i can get entity managers(multiple databases) and how to create schema's i need to connect?

Please help me.

After reading full doc of sap hana cloud platform i figured it out, i deployed my war file in java application section of sap hana cockpit. By JPA 2.0 i connected to SAP DB ( source to create DB and binding is ) and to get entity manager object below code helped a lot

    import javax.sql.DataSource;
    import javax.annotation.Resource;
    import javax.annotation.Resources;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    import javax.persistence.EntityManager;
    import javax.persistence.EntityManagerFactory;
    import javax.persistence.Persistence;
    import javax.servlet.annotation.WebServlet;

{
    DataSource ds =null;
                          EntityManagerFactory emf;
                        InitialContext ctx = null;
                            try {
                                ctx = new InitialContext();
                            } catch (NamingException e) {
                                e.printStackTrace();
                            }
                          try {
        //Note: jdbc/fbk is data source name which we'll give at binding database, by default it will be as "<default>" edit that to jdbc/fbk or else anything
                                ds = (DataSource) ctx.lookup("java:comp/env/jdbc/fbk");
                            } catch (NamingException e) {
                                e.printStackTrace();
                            }

                          @SuppressWarnings("rawtypes")
                            Map properties = new HashMap();
                          properties.put(PersistenceUnitProperties.JTA_DATASOURCE, ds);
                        properties.put(PersistenceUnitProperties.DDL_GENERATION,
                                PersistenceUnitProperties.CREATE_OR_EXTEND);
                        properties.put(PersistenceUnitProperties.DDL_GENERATION_MODE,
                                PersistenceUnitProperties.DDL_DATABASE_GENERATION);
                          emf = Persistence.createEntityManagerFactory("pu", properties);
                          EntityManager em = emf.createEntityManager();
return em;
}

persistence.xml code is:

<?xml version="1.0" encoding="UTF-8"?>
<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_2_0.xsd"
    version="2.0">

     <persistence-unit name="pu" transaction-type="JTA">
     <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
     <class>Vaadin_Sample.hell.FunctionKey</class>

         <properties>
            </properties>
       </persistence-unit>
    </persistence>

Resouce mapping is done with the help of below annotation:

 @WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
    @VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
    @Resource(name="jdbc/fbk",type=DataSource.class)
    public static class MyUIServlet extends VaadinServlet {
    }

Note:The above all process done by trial account OF COCKPIT only.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM