|
| 1 | +package com.baeldung.reladomo; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.nio.file.Files; |
| 5 | +import java.nio.file.Path; |
| 6 | +import java.nio.file.Paths; |
| 7 | +import java.sql.Connection; |
| 8 | +import java.sql.SQLException; |
| 9 | +import java.util.TimeZone; |
| 10 | +import java.util.stream.Stream; |
| 11 | + |
| 12 | +import org.h2.tools.RunScript; |
| 13 | + |
| 14 | +import com.gs.fw.common.mithra.bulkloader.BulkLoader; |
| 15 | +import com.gs.fw.common.mithra.bulkloader.BulkLoaderException; |
| 16 | +import com.gs.fw.common.mithra.connectionmanager.SourcelessConnectionManager; |
| 17 | +import com.gs.fw.common.mithra.connectionmanager.XAConnectionManager; |
| 18 | +import com.gs.fw.common.mithra.databasetype.DatabaseType; |
| 19 | +import com.gs.fw.common.mithra.databasetype.H2DatabaseType; |
| 20 | + |
| 21 | +public class ReladomoConnectionManager implements SourcelessConnectionManager { |
| 22 | + |
| 23 | + private static ReladomoConnectionManager instance; |
| 24 | + |
| 25 | + private XAConnectionManager xaConnectionManager; |
| 26 | + |
| 27 | + private final String databaseName = "myDb"; |
| 28 | + |
| 29 | + public static synchronized ReladomoConnectionManager getInstance() { |
| 30 | + if (instance == null) { |
| 31 | + instance = new ReladomoConnectionManager(); |
| 32 | + } |
| 33 | + return instance; |
| 34 | + } |
| 35 | + |
| 36 | + private ReladomoConnectionManager() { |
| 37 | + this.createConnectionManager(); |
| 38 | + } |
| 39 | + |
| 40 | + private XAConnectionManager createConnectionManager() { |
| 41 | + xaConnectionManager = new XAConnectionManager(); |
| 42 | + xaConnectionManager.setDriverClassName("org.h2.Driver"); |
| 43 | + xaConnectionManager.setJdbcConnectionString("jdbc:h2:mem:" + databaseName); |
| 44 | + xaConnectionManager.setJdbcUser("sa"); |
| 45 | + xaConnectionManager.setJdbcPassword(""); |
| 46 | + xaConnectionManager.setPoolName("My Connection Pool"); |
| 47 | + xaConnectionManager.setInitialSize(1); |
| 48 | + xaConnectionManager.setPoolSize(10); |
| 49 | + xaConnectionManager.initialisePool(); |
| 50 | + return xaConnectionManager; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public BulkLoader createBulkLoader() throws BulkLoaderException { |
| 55 | + return null; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public Connection getConnection() { |
| 60 | + return xaConnectionManager.getConnection(); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public DatabaseType getDatabaseType() { |
| 65 | + return H2DatabaseType.getInstance(); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public TimeZone getDatabaseTimeZone() { |
| 70 | + return TimeZone.getDefault(); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public String getDatabaseIdentifier() { |
| 75 | + return databaseName; |
| 76 | + } |
| 77 | + |
| 78 | + public void createTables() throws Exception { |
| 79 | + Path ddlPath = Paths.get(ClassLoader.getSystemResource("sql").toURI()); |
| 80 | + |
| 81 | + try (Connection conn = xaConnectionManager.getConnection(); Stream<Path> list = Files.list(ddlPath);) { |
| 82 | + list.forEach(path -> { |
| 83 | + try { |
| 84 | + RunScript.execute(conn, Files.newBufferedReader(path)); |
| 85 | + } catch (SQLException | IOException exc) { |
| 86 | + exc.printStackTrace(); |
| 87 | + } |
| 88 | + }); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | +} |
0 commit comments