JPA + Wildfly + Hibernate

4

Is there a way to dynamically change the connection to the database by changing the standalone.xml file of Wildfly 8 (or 10) ? Currently I have a HibernateSessionFactory class that creates a m EntityManagerFactory with the connection parameters from a .properties file:

    try {
        Map<String, String> parametros = new HashMap<String, String>();
        String valor = "jdbc:sqlserver:" + pGetServer()
                + ";databaseName=%s";
        parametros.put(PERSISTENCE, String.format(valor, pGetDataBase()));
        entityManagerFactory = Persistence.createEntityManagerFactory(
                "BANCO", parametros);
    } catch (Exception e) {
        System.err.println("%%%% Error Creating entityManagerFactory %%%%");
        e.printStackTrace();
    }

persistence.xml :

<properties>
        <property name="javax.persistence.jdbc.driver"
            value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
        <property name="javax.persistence.jdbc.user" value="user" />
        <property name="javax.persistence.jdbc.password" value="pass" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" />
        <property name="hibernate.show_sql" value="false" />
        <property name="hibernate.format_sql" value="true" />
        <property name="hibernate.cache.provider_class"
            value="org.hibernate.cache.SingletonEhCacheProvider" />
    </properties>
    
asked by anonymous 03.01.2018 / 11:07

3 answers

0
<!-- Conexao com SQLServer -->
<persistence-unit name="nome-persistence1" transaction-type="JTA">

    <jta-data-source>conexao1</jta-data-source>

     <class>entidade</class>

   <properties>
        <property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
        <property name="javax.persistence.jdbc.user" value="user" />
        <property name="javax.persistence.jdbc.password" value="pass" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" />
        <property name="hibernate.show_sql" value="false" />
        <property name="hibernate.format_sql" value="true" />
        <property name="hibernate.cache.provider_class"
            value="org.hibernate.cache.SingletonEhCacheProvider" />
    </properties>

</persistence-unit>

<!-- Conexao com oracle -->
<persistence-unit name="nome-persistence2" transaction-type="JTA">

    <jta-data-source>java:/conexao2</jta-data-source>

     <class>entidade</class>

   <properties>
        <property name="javax.persistence.jdbc.driver" value="Oracle10gDialect" />
        <property name="javax.persistence.jdbc.user" value="user" />
        <property name="javax.persistence.jdbc.password" value="pass" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
        <property name="hibernate.show_sql" value="false" />
        <property name="hibernate.format_sql" value="true" />
    </properties>

</persistence-unit>

You should point to your DataSource Wildfly 8 configuration ! That should stay more or less to yes depending on your application server.

<datasource jta="true" jndi-name="java:/conexao1" pool-name="conexao1" enabled="true" use-java-context="true" use-ccm="true">
    <connection-url>url</connection-url>
    <driver>SQLServerDriver</driver>
    <!--configuração do DataSource do  Wildfly 8 -->
</datasource>

<datasource jta="true" jndi-name="java:/conexao2" pool-name="conexao2" enabled="true" use-java-context="true" use-ccm="true">
    <connection-url>url</connection-url>
    <driver>oracleDriver</driver>
    <!--configuração do DataSource do  Wildfly 8 -->
</datasource>
    
04.01.2018 / 12:48
0
  

Is there any way to dynamically change the connection to the database by changing the standalone.xml file for Wildfly 8 (or 10)?

If I understand correctly, you want to work with more than one database, right?

If yes, I find it easier to create a datasource in standalone.xml and a persistence unit in persistence.xml for each connection separately and choose which to work with when creating EntityManagerFactory .

For example:

standalone.xml:

<datasource jndi-name="java:jboss/datasources/dataSource1" pool-name="dataSource1">
    <connection-url>jdbc:mysql:localhost:3306/banco1</connection-url>
    <driver>com.mysql.jdbc.Driver</driver>
    <security>
        <user-name>usuario1</user-name>
        <password>senha1</password>
    </security>
</datasource>

<datasource jndi-name="java:jboss/datasources/dataSource2" pool-name="dataSource2">
    <connection-url>jdbc:mysql:localhost:3306/banco2</connection-url>
    <driver>com.mysql.jdbc.Driver</driver>
    <security>
        <user-name>usuario2</user-name>
        <password>senha2</password>
    </security>
</datasource>

persistence.xml:

<persistence-unit name="persistenceUnit1">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <non-jta-data-source>java:comp/env/jboss/datasources/dataSource1</non-jta-data-source>
</persistence-uni>

<persistence-unit name="persistenceUnit2">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <non-jta-data-source>java:comp/env/jboss/datasources/dataSource2</non-jta-data-source>
</persistence-uni>

JPA Utility Class:

if (condicao) {
    factory = Persistence.createEntityManagerFactory("persistenceUnit1"); 
} else {
    factory = Persistence.createEntityManagerFactory("persistenceUnit2"); 
}
    
04.01.2018 / 16:34
0

Thank you guys, for the help! I solved it as follows:

  • I added the default connection in the persistence.xml file:

    <persistence-unit name="Persistence-unit">
        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:sqlserver:SERVIDORX;databaseName=BASEX" />
            <property name="javax.persistence.jdbc.user" value="USER" />
            <property name="javax.persistence.jdbc.password" value="PASSWORD" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" />
            <property name="hibernate.cache.provider_class" value="org.hibernate.cache.SingletonEhCacheProvider" />
        </properties>
    </persistence-unit>
    
  • I added a datasource to the standalone.xml file:

            <datasource jndi-name="java:jboss/datasources/DataSource" pool-name="DataSource" enabled="true" use-java-context="true">
                <connection-url>jdbc:sqlserver:SERVIDORY;databaseName=BANCOY;DB_CLOSE_ON_EXIT=FALSE</connection-url>
                <driver>sqlServer</driver>
                <security>
                    <user-name>USER</user-name>
                    <password>PASSWORD</password>
                </security>
            </datasource>
            <drivers>
                ...
                <driver name="sqlServer" module="jdbc.sqlServer">
                    <xa-datasource-class>com.microsoft.sqlserver.jdbc.SQLServerXADataSource</xa-datasource-class>
                </driver>
            </drivers>
    
  • In the Hibernate class:

    Context contexto = new InitialContext();
    Connection conexao = null;
    DataSource dataSource = null;
    String server = "";
    String dataBase = "";
    try {   
        dataSource = (DataSource)contexto.lookup("java:jboss/datasources/DataSource");
        conexao = dataSource.getConnection();
        server = conexao.getMetaData().getURL().split(";")[0].split(":")[2];
        dataBase = conexao.getMetaData().getURL().split(";")[15].split("=")[1];
        Map<String, String> parametros = new HashMap<String, String>();
        parametros.put("javax.persistence.jdbc.url","jdbc:sqlserver:" + server + ";databaseName=" + dataBase);
        entityManagerFactory = Persistence.createEntityManagerFactory("Persistence-unit",parametros);
    } catch (Exception e) {
        entityManagerFactory = Persistence.createEntityManagerFactory("Persistence-unit");
    } finally {
        if ((conexao != null) && (!conexao.isClosed())) {
            conexao.close();
        }
    }
    
05.01.2018 / 15:52