JPA + Wildfly + Hibernate

Asked

Viewed 1,175 times

5

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

    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>
  • Post your . properties, think you can read the file dynamically. It’s change your connection. In which situation you want to change your connection.

  • That’s the point. I already got the connection from there. The idea is not to get the connection information from there, but from Wildfly’s standalone.xml file.

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 configuration of Datasource for Wildfly 8! Which should be at least a 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>
  • You can do yes if it helps you!!

0

Thanks guys, for the help! I decided as follows:

  • I added the default connection to 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();
        }
    }
    

0

Is there any way to dynamically change the database connection by changing the Wildfly 8 standalone.xml file (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 on standalone.xml and a persistence unit in the persistence.xml for each connection separately and choose which one to work with when creating the 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"); 
}
  • I solved it in a different way, but that would also be a great option! ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.