1

To me it looks like autocommit is completely overridden with Spring-Hibernate configuration and this property absolutely doesn't play any role in such a configuration but I would like to confirm that somehow.

Spring boot 1.5.10.RELEASE version. Database is PostgreSQL 9.5.6.

Datasource configuration

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;

import javax.persistence.EntityManagerFactory;
import java.util.Properties;

@Configuration
public class DataSourceConfig {

    private Logger logger = LoggerFactory.getLogger(DataSourceConfig.class);

    @Bean(name = "dataSource")
    public DriverManagerDataSource dataSource() {
        DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
        driverManagerDataSource.setDriverClassName(driver);
        driverManagerDataSource.setUrl(url);
        driverManagerDataSource.setUsername(username);
        driverManagerDataSource.setPassword(password);
        return driverManagerDataSource;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource());
        em.setPackagesToScan(new String[] { domainModelNamespace });
        em.setJpaProperties(additionalProperties());

        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);

        return em;
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
        return new PersistenceExceptionTranslationPostProcessor();
    }

    @Bean
    public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(emf);

        return transactionManager;
    }

    Properties additionalProperties() {
        Properties properties = new Properties();
        properties.setProperty("hibernate.show_sql", showSql);
        properties.setProperty("hibernate.dialect", dialect);
        properties.setProperty("hibernate.hbm2ddl.auto", hbm2ddl);
        return properties;
    }
}

And here is test method

import com.phonebook.IntegrationTest;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.sql.DataSource;
import java.sql.SQLException;

public class DataSourceTesting extends IntegrationTest{

    @Autowired
    private DataSource dataSource;

    Logger logger = LoggerFactory.getLogger(DataSourceTesting.class);

    @Test
    public void testDataSourceConnectionProperties() throws SQLException {
        boolean autoCommit = dataSource.getConnection().getAutoCommit();
        Assert.assertFalse(autoCommit);
    }

}

The question is what autocommit means in this context? In test it is on

true

as well as in @Transactional method checking it in debug mode.

mommcilo
  • 916
  • 10
  • 25
  • Possible duplicate of [How to set autocommit to false in spring jdbc template](https://stackoverflow.com/questions/25197774/how-to-set-autocommit-to-false-in-spring-jdbc-template) – user7294900 Feb 12 '18 at 15:55
  • Well not sure. My question is not How it is rather "Should that be done and why" – mommcilo Feb 12 '18 at 16:07
  • 3
    Should it be done, no and it will have no effect. You are using a transaction manager which will disable auto-commit always. Auto commit with transactions doesn't make sense as that basically destroys the transaction (each statement would then be a separate transaction). – M. Deinum Feb 13 '18 at 09:27

0 Answers0