user3748908
user3748908

Reputation: 905

Different ways of setting DB connection properties

When trying to set connectionProperties to "useUnicode=yes;characterEncoding=utf8;", is there any difference between this:

@Bean
public DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    Properties properties = new Properties();
    properties.setProperty("useUnicode", true);
    properties.setProperty("characterEncoding", "UTF-8"); // UTF-8 or utf8?
    dataSource.setConnectionProperties(properties);
    return dataSource;
}

And this?

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean entityManagerFactory =
            new LocalContainerEntityManagerFactoryBean();
    Properties properties = new Properties();
    properties.setProperty("hibernate.connection.useUnicode", true);
    properties.setProperty("hibernate.connection.characterEncoding", "UTF-8"); // UTF-8 or utf8?
    entityManagerFactory.setJpaProperties(properties);
    return entityManagerFactory;
}

Or even a third way, just adding them 'raw' to the URL:

jdbc:mysql://localhost:3306/?useUnicode=yes&characterEncoding=UTF-8

Upvotes: 2

Views: 1512

Answers (1)

ivanenok
ivanenok

Reputation: 614

As I know there is no difference in these methods of passing connection parameters.

  1. There is configuration parameters of a datasource, they will be passing to 'DriverManager.getConnection()' on connection creation.

  2. All connection properties 'hibernate.connection.' are using for passing to 'DriverManager.getConnection()' too. In most cases it will be internal passing of properties to datasource as in the first scenario.

  3. Yes, you can pass all required parameters by declaring them in jdbc url.

Upvotes: 1

Related Questions