简体   繁体   English

SAP HANA Cloud如何支持多个数据库?

[英]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). 我有一个场景,我需要在SAP HANA Cloud平台(目标运行时为JAVA Web)中部署我的vaadin应用程序(在运行时间为tomcat 7的多个数据库(2个持久性单元)上正常工作)。

My earlier database connection was with MYSQL by using eclipselink in below way 我以前的数据库连接是通过以下方式使用eclipselink与MYSQL建立的

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. 上面的方法有助于连接到数据库(MYSQL)并返回实体管理器。

The same methodology i need in my application which is gonna deploy in SAP HANA CLOUD. 我需要在应用程序中使用的相同方法,该方法将部署在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. 阅读了sap hana云平台的完整文档后,我发现了问题,然后将我的war文件部署在sap hana座舱的Java应用程序部分中。 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 通过JPA 2.0,我连接到SAP DB( 创建数据库和绑定的源 )并在代码下获取实体管理器对象有很大帮助

    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: persistence.xml代码是:

<?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. 注意:以上所有程序仅由试用帐户OF COCKPIT完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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