|
| 1 | +package com.baeldung.jtademo; |
| 2 | + |
| 3 | +import com.baeldung.jtademo.services.TellerService; |
| 4 | +import org.hsqldb.jdbc.JDBCDataSourceFactory; |
| 5 | +import org.hsqldb.jdbc.pool.JDBCXADataSource; |
| 6 | +import org.springframework.beans.factory.annotation.Autowired; |
| 7 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 8 | +import org.springframework.boot.CommandLineRunner; |
| 9 | +import org.springframework.boot.SpringApplication; |
| 10 | +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
| 11 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 12 | +import org.springframework.boot.jta.bitronix.BitronixXADataSourceWrapper; |
| 13 | +import org.springframework.boot.jta.bitronix.PoolingDataSourceBean; |
| 14 | +import org.springframework.context.annotation.Bean; |
| 15 | +import org.springframework.core.io.DefaultResourceLoader; |
| 16 | +import org.springframework.core.io.Resource; |
| 17 | +import org.springframework.jdbc.core.JdbcTemplate; |
| 18 | +import org.springframework.jdbc.core.ResultSetExtractor; |
| 19 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; |
| 20 | +import org.springframework.jdbc.datasource.init.ScriptUtils; |
| 21 | +import org.springframework.transaction.annotation.EnableTransactionManagement; |
| 22 | +import org.springframework.util.Assert; |
| 23 | + |
| 24 | +import javax.sql.DataSource; |
| 25 | +import java.math.BigDecimal; |
| 26 | +import java.sql.Connection; |
| 27 | +import java.sql.SQLException; |
| 28 | + |
| 29 | +@EnableAutoConfiguration |
| 30 | +@EnableTransactionManagement |
| 31 | +public class JtaDemoApplication { |
| 32 | + |
| 33 | + @Bean("dataSourceAccount") |
| 34 | + public DataSource dataSource() throws Exception { |
| 35 | + return createHsqlXADatasource("jdbc:hsqldb:mem:accountDb"); |
| 36 | + } |
| 37 | + |
| 38 | + @Bean("dataSourceAudit") |
| 39 | + public DataSource dataSourceAudit() throws Exception { |
| 40 | + return createHsqlXADatasource("jdbc:hsqldb:mem:auditDb"); |
| 41 | + } |
| 42 | + |
| 43 | + private DataSource createHsqlXADatasource(String connectionUrl) throws Exception { |
| 44 | + JDBCXADataSource dataSource = new JDBCXADataSource(); |
| 45 | + dataSource.setUrl(connectionUrl); |
| 46 | + dataSource.setUser("sa"); |
| 47 | + BitronixXADataSourceWrapper wrapper = new BitronixXADataSourceWrapper(); |
| 48 | + return wrapper.wrapDataSource(dataSource); |
| 49 | + } |
| 50 | + |
| 51 | + @Bean("jdbcTemplateAccount") |
| 52 | + public JdbcTemplate jdbcTemplate(@Qualifier("dataSourceAccount") DataSource dataSource) { |
| 53 | + return new JdbcTemplate(dataSource); |
| 54 | + } |
| 55 | + |
| 56 | + @Bean("jdbcTemplateAudit") |
| 57 | + public JdbcTemplate jdbcTemplateAudit(@Qualifier("dataSourceAudit") DataSource dataSource) { |
| 58 | + return new JdbcTemplate(dataSource); |
| 59 | + } |
| 60 | +} |
0 commit comments