How can I reconnect to the database by putting variables on standalone instead of in Java code

Asked

Viewed 42 times

0

I need to include the variables urlLot, user and pass in the standalone so you don’t have to change the code and recompile every time I switch environments. Unfortunately I can’t think of any way to do that without having to change the whole structure of the project.

Follows how is my connection class with the database:

public Connection getConexao() throws SQLException{
        String urlLot = "jdbc:postgresql://localhost:5432/teste";
        String user      =  "postgres";
        String pass      =  "123";

        try {           
            if(this.connection == null || this.connection.isClosed()){
                Class.forName("org.postgresql.Driver");             
                this.connection = DriverManager.getConnection(urlLot, user, pass);     
                PreparedStatement s = connection.prepareStatement("set search_path=db_teste;"); 
                s.execute();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return this.connection;
    }
}

1 answer

0

I understand you plan to use the wildfly or jboss, giving him the responsibility of your connection, in which case you need to:

  1. Configure the application server: in the link below I explain how to do this in wildfly for a connection using oracle: https://medium.com/@fmotta.antonio/configuring-datasource-oracle-no-wildfly-13f382d4bb3d
  2. Prepare your application to use JTA, this is done via JPA, so you need to understand how to apply JPA in your preferred application using Hibernate, a nice tutorial on how to do this can be found in the following link:https://blog.algaworks.com/tutorial-jpa/ and https://www.devmedia.com.br/jpa-e-hibernate-acessando-dados-em-aplicacoes-java/32711
  3. Finally, the cat jump that is pass the responsibility of connection to the application server: The tutorials above teach you to create a persistence with RESOURCE_LOCAL, IE, you still keep putting the database settings in the application, to pass this responsibility to the server just change the following setting in persistence:
<persistence-unit name="nomeDaPU" transaction-type="RESOURCE_LOCAL">

to JTA, and add the property jta-data-source, as in the example below:

<?xml version="1.0" encoding="UTF-8" ?>
<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" xmlns="http://java.sun.com/xml/ns/persistence">

    <persistence-unit name="nomeDaPU" transaction-type="JTA">
       <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>java:/nomeDoDataSourceConfiguradoNoServidor</jta-data-source>
    </persistence-unit>
</persistence>

I suggest you do it step by step in Poc form (proof of concept), implement JPA using RESOURCE_LOCAL, then try to pass the responsibility to the application server.

Browser other questions tagged

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