Spring交易无效的Neo4j-Jdbc

时间:2016-05-03 04:24:36

标签: spring jdbc neo4j transactions spring-data-neo4j-4

我在Spring 4+上使用neo4j-jdbc-2.3.2。对于CRUD,我正在使用JDBCTemplate。

我正面临两个挑战

  1. 交易无效。抛出Checked或Unchecked异常不会回滚事务并在Neo4j中保留数据
  2. JDBCTemplate具有使用KeyHolder类返回自动生成ID的功能。这不适用于Neo4j。它给了我Null Pointer。
  3. 这是一个小测试用例

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = { Neo4jJdbcSpike.Config.class })
    @Transactional
    @Rollback(false)
    @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
    public class Neo4jJdbcSpike {
    
        @Autowired
        private JdbcTemplate jdbcTemplate;
    
        @Test
        public void transactionManagementNotWorkingTest() {
            String cypher = "create (a:Person { name: ?})";
            jdbcTemplate.update(cypher, new Object[] { "Foo" });
            throw new IllegalStateException("This will not rollback trnsaction");
        }
    
        @Test
        public void transactionManagementNotWorkingCheckedExceptioTest() throws CheckedException {
            String cypher = "create (a:Person { name: ?})";
            jdbcTemplate.update(cypher, new Object[] { "Bar" });
            throw new CheckedException();
        }
    
        @Test
        public void getGeneratedGraphIDTest() throws CheckedException {
            String cypher = "create (a:Person { name: ?})";
            KeyHolder holder = new GeneratedKeyHolder();
            PreparedStatementCreator psc = new PreparedStatementCreator() {
    
                @Override
                public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
                    PreparedStatement ps = con.prepareStatement(cypher, Statement.RETURN_GENERATED_KEYS);
                    ps.setString(1, "Bar");
                    return ps;
                }
            };
            jdbcTemplate.update(psc, holder);
            Assert.assertNotNull(holder.getKey());
            Assert.assertTrue(holder.getKey().longValue() > 0L);
        }
    
        public static class CheckedException extends Exception {
    
        }
    
        @Configuration
        @Import(TestConfig.class)
        @EnableTransactionManagement
        static class Config {
    
            @Bean
            public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
                return new PropertySourcesPlaceholderConfigurer();
            }
    
            @Bean
            public DataSource dataSource() {
                return new DriverManagerDataSource("jdbc:neo4j://localhost:7474", "neo4j", "passw0rd");
            }
    
            @Bean
            public JdbcTemplate jdbcTemplate(DataSource dataSource) {
                return new JdbcTemplate(dataSource);
            }
    
            @Bean
            public DataSourceTransactionManager txManager(DataSource dataSource) {
                return new DataSourceTransactionManager(dataSource);
            }
    
        }
    
    }
    

    注意:所有测试用例都失败了...... 在此先感谢....

0 个答案:

没有答案