|
| 1 | +package com.metaobjects.spring; |
| 2 | + |
| 3 | +import com.metaobjects.manager.ObjectConnection; |
| 4 | +import org.junit.Before; |
| 5 | +import org.junit.Test; |
| 6 | +import org.junit.runner.RunWith; |
| 7 | +import org.springframework.beans.factory.annotation.Autowired; |
| 8 | +import org.springframework.context.annotation.Bean; |
| 9 | +import org.springframework.context.annotation.Configuration; |
| 10 | +import org.springframework.jdbc.datasource.DataSourceTransactionManager; |
| 11 | +import org.springframework.jdbc.datasource.DataSourceUtils; |
| 12 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; |
| 13 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; |
| 14 | +import org.springframework.test.context.ContextConfiguration; |
| 15 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 16 | +import org.springframework.transaction.PlatformTransactionManager; |
| 17 | +import org.springframework.transaction.support.TransactionTemplate; |
| 18 | + |
| 19 | +import javax.sql.DataSource; |
| 20 | +import java.sql.Connection; |
| 21 | +import java.sql.PreparedStatement; |
| 22 | +import java.sql.ResultSet; |
| 23 | +import java.sql.Statement; |
| 24 | + |
| 25 | +import static org.junit.Assert.*; |
| 26 | + |
| 27 | +/** |
| 28 | + * Verifies that SpringObjectConnections joins the caller's Spring-managed transaction: |
| 29 | + * <ol> |
| 30 | + * <li>The ObjectConnection wraps the SAME physical connection Spring bound to the tx.</li> |
| 31 | + * <li>close() on the wrapper is a no-op — Spring owns the lifecycle.</li> |
| 32 | + * <li>DML executed through the connection participates in Spring rollback.</li> |
| 33 | + * </ol> |
| 34 | + */ |
| 35 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 36 | +@ContextConfiguration(classes = SpringObjectConnectionTest.TestConfig.class) |
| 37 | +public class SpringObjectConnectionTest { |
| 38 | + |
| 39 | + @Configuration |
| 40 | + static class TestConfig { |
| 41 | + |
| 42 | + @Bean |
| 43 | + public DataSource dataSource() { |
| 44 | + return new EmbeddedDatabaseBuilder() |
| 45 | + .setType(EmbeddedDatabaseType.H2) |
| 46 | + .build(); |
| 47 | + } |
| 48 | + |
| 49 | + @Bean |
| 50 | + public PlatformTransactionManager transactionManager(DataSource dataSource) { |
| 51 | + return new DataSourceTransactionManager(dataSource); |
| 52 | + } |
| 53 | + |
| 54 | + @Bean |
| 55 | + public TransactionTemplate transactionTemplate(PlatformTransactionManager transactionManager) { |
| 56 | + return new TransactionTemplate(transactionManager); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Autowired |
| 61 | + private DataSource dataSource; |
| 62 | + |
| 63 | + @Autowired |
| 64 | + private TransactionTemplate transactionTemplate; |
| 65 | + |
| 66 | + @Before |
| 67 | + public void createTable() throws Exception { |
| 68 | + try (Connection c = dataSource.getConnection(); |
| 69 | + Statement st = c.createStatement()) { |
| 70 | + st.execute("CREATE TABLE IF NOT EXISTS t (id INT PRIMARY KEY)"); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Test 1 — same connection + no-close. |
| 76 | + * |
| 77 | + * Inside a Spring transaction, SpringObjectConnections.current() must return the |
| 78 | + * exact same physical Connection that Spring bound to the transaction. After |
| 79 | + * calling close() on the wrapper the underlying connection must still be open |
| 80 | + * (Spring, not the wrapper, owns the lifecycle). |
| 81 | + */ |
| 82 | + @Test |
| 83 | + public void testJoinsTransactionAndCloseIsNoOp() throws Exception { |
| 84 | + transactionTemplate.execute(status -> { |
| 85 | + try { |
| 86 | + ObjectConnection oc = SpringObjectConnections.current(dataSource); |
| 87 | + Connection springConn = DataSourceUtils.getConnection(dataSource); |
| 88 | + |
| 89 | + // The wrapped connection must be the same physical object Spring bound. |
| 90 | + Connection wrappedConn = (Connection) oc.getDatastoreConnection(); |
| 91 | + assertSame( |
| 92 | + "SpringObjectConnections must return the tx-bound connection", |
| 93 | + springConn, wrappedConn); |
| 94 | + |
| 95 | + // close() must be a no-op — Spring owns the lifecycle. |
| 96 | + oc.close(); |
| 97 | + assertFalse( |
| 98 | + "Underlying connection must still be open after wrapper close()", |
| 99 | + wrappedConn.isClosed()); |
| 100 | + |
| 101 | + // Release the extra reference obtained above (DataSourceUtils contract). |
| 102 | + DataSourceUtils.releaseConnection(springConn, dataSource); |
| 103 | + } catch (Exception e) { |
| 104 | + throw new RuntimeException(e); |
| 105 | + } |
| 106 | + return null; |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Test 2 — rollback participation. |
| 112 | + * |
| 113 | + * DML executed via the connection obtained from SpringObjectConnections.current() |
| 114 | + * must be rolled back when the surrounding Spring transaction rolls back. |
| 115 | + * After the rollback the row must be absent. |
| 116 | + */ |
| 117 | + @Test |
| 118 | + public void testRollbackRemovesRow() throws Exception { |
| 119 | + // Execute inside a transaction that we force to roll back. |
| 120 | + try { |
| 121 | + transactionTemplate.execute(status -> { |
| 122 | + try { |
| 123 | + ObjectConnection oc = SpringObjectConnections.current(dataSource); |
| 124 | + Connection conn = (Connection) oc.getDatastoreConnection(); |
| 125 | + try (PreparedStatement ps = conn.prepareStatement("INSERT INTO t VALUES (1)")) { |
| 126 | + ps.executeUpdate(); |
| 127 | + } |
| 128 | + } catch (Exception e) { |
| 129 | + throw new RuntimeException(e); |
| 130 | + } |
| 131 | + // Force rollback by throwing an unchecked exception. |
| 132 | + throw new RuntimeException("intentional rollback"); |
| 133 | + }); |
| 134 | + } catch (RuntimeException ignored) { |
| 135 | + // expected — transaction has been rolled back |
| 136 | + } |
| 137 | + |
| 138 | + // Verify the row is absent on a fresh, non-transactional connection. |
| 139 | + try (Connection c = dataSource.getConnection(); |
| 140 | + Statement st = c.createStatement(); |
| 141 | + ResultSet rs = st.executeQuery("SELECT COUNT(*) FROM t WHERE id = 1")) { |
| 142 | + assertTrue(rs.next()); |
| 143 | + assertEquals( |
| 144 | + "Row must be absent after rollback — OMDB connection participated in the Spring tx", |
| 145 | + 0, rs.getInt(1)); |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments