A
A
AlexanderM-O2019-12-18 12:39:17
Java
AlexanderM-O, 2019-12-18 12:39:17

What components and data are needed for the HibernateTransactionManager to function?

For the normal operation of the Manager, you have to set the DataSource

public class DataBaseConfig {
    private static final String url="jdbc:mysql://localhost:3306/BASE?useLegacyDatetimeCode=false&serverTimezone=UTC";
    private static final String driverClassName="com.mysql.cj.jdbc.Driver";
    private static final String userName="root";
    private static final String password="1234";
    
    @Bean
    public DataSource dataSource() {
    	BasicDataSource ds=new BasicDataSource();
    	ds.setUrl(url);
    	ds.setDriverClassName(driverClassName);
    	ds.setUsername(userName);
    	ds.setPassword(password);
    return ds;
    }
    }
  
  @Bean
    public SessionFactory sessionFactory() throws IOException {
    LocalSessionFactoryBean sessionFactoryBean=new LocalSessionFactoryBean();
    sessionFactoryBean.setConfigLocation(new ClassPathResource("hibernate.cfg.xml"));
    sessionFactoryBean.setPackagesToScan("ru.cource.model.domain");
    sessionFactoryBean.afterPropertiesSet();
        return sessionFactoryBean.getObject();
        
    }
    
    @Bean
    public PlatformTransactionManager transactionManager() throws IOException {		
    	HibernateTransactionManager transactionManager = new HibernateTransactionManager();
    	transactionManager.setDataSource(dataSource());
    	transactionManager.setSessionFactory(sessionFactory());
    	return transactionManager; 
    }

When deleting the DataSource I get UnknownUnwrapTypeException: Cannot unwrap to requested type [javax.sql.DataSource] .
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>

        <property name="connection.url" >jdbc:mysql://localhost:3306/BASE?useLegacyDatetimeCode=false&amp;serverTimezone=UTC</property>
        <property name="connection.driver_class" >com.mysql.cj.jdbc.Driver</property>
        <property name="connection.username">root</property>
        <property name="connection.password">1234</property>

        <property name="show_sql">true</property>


        <property name="hibernate.hbm2ddl.auto">validate</property >
        <property name="hibernate.dialect" >org.hibernate.dialect.MySQL8Dialect</property >
    </session-factory>
</hibernate-configuration>

Is it possible to do without creating a duplicate property file for the DataSource configuration?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2020-01-15
@Terran37

Your dataSource method returns a DataSource. You don't need to delete. You either take the connections as it is now from the variables in the code, or you can read from the property file. You choose.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question