79590064

Date: 2025-04-24 07:56:51
Score: 0.5
Natty:
Report link
My question is how to initialize jdbcEnvironment properly?

You might want to provide more sample code to make the issue more reproducible. but I am fine tuning JDBC URL parameters could receive more helpful answers if more relevant code is provided to let people reproduce the exact issue.

The documentation says,

Ensuring that the database initializer is initialized first can also be easy. Some suggestions on how to implement this include:

Rely on the default behavior of the Spring BeanFactory, which is that beans are initialized in registration order.

I produced an example which I tested to behave in the order of handling the DataSource first. I added the waiting time of 10 seconds intentionally to simulate the network waiting time or decryption processing time:

    public static String getPassword(){
        String passwordAfterWaitingSimulation = "";
        try{
            System.out.println("Start waiting.");
            for (int i = 0; i < 10; i++){
                Thread.sleep(1000);
                System.out.println("Waiting for " + i + " second(s).");
            }
            System.out.println("Stop waiting.");
            passwordAfterWaitingSimulation = "YourPassword";
        } catch (Exception e){}
        
        return passwordAfterWaitingSimulation;
    }

The SQLServerDataSource:

    @Bean
    DataSource dataSource() {
        final SQLServerDataSource dataSource = new SQLServerDataSource();
        String url = DbConnectionSetUp.getURL();
        String user = DbConnectionSetUp.getUser();
        String password = DbConnectionSetUp.getPassword();
        dataSource.setURL(url);
        dataSource.setUser(user);
        dataSource.setPassword(password);

        // clearing after use
        url = null;
        user = null;
        password = null;
        return dataSource;
    }

I used dependency injection which essentially created the order of handling DataSource dataSource first in the code:

    public DataService(DataSource dataSource){
        this.dataSource = dataSource;
    }

After .\mvnw spring-boot:run, when I opened http://localhost:8080/ in the browser, I saw the expected results of the inserted data, e.g.

1
user ä ö ü 0
2
user ä ö ü 1
3
user ä ö ü 2
4
user ä ö ü 3
5
user ä ö ü 4
6
user ä ö ü 5
7
user ä ö ü 6
8
user ä ö ü 7
9
user ä ö ü 8
10
user ä ö ü 9

The repository of this example is here.

Reasons:
  • Contains signature (1):
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Hdvlp