/** * Create a new sql object which will obtain and release connections from this dbi instance, as it needs to, * and can, respectively. You should not explicitely close this sql object * * @param sqlObjectType an interface with annotations declaring desired behavior * @param <SqlObjectType> * @return a new sql object of the specified type, with a dedicated handle */ public <SqlObjectType> SqlObjectType onDemand(Class<SqlObjectType> sqlObjectType) { return SqlObjectBuilder.onDemand(this, sqlObjectType); }
/** * Create a new sql object which will obtain and release connections from this dbi instance, as it needs to, * and can, respectively. You should not explicitely close this sql object * * @param sqlObjectType an interface with annotations declaring desired behavior * @param <SqlObjectType> * @return a new sql object of the specified type, with a dedicated handle */ @Override public <SqlObjectType> SqlObjectType onDemand(Class<SqlObjectType> sqlObjectType) { return SqlObjectBuilder.onDemand(this, sqlObjectType); }
@Override public <SqlObjectType> SqlObjectType onDemand(final Class<SqlObjectType> sqlObjectType) { if (delegate == null) { throw new IllegalStateException("No delegate has been set!"); } return wrapping ? SqlObjectBuilder.onDemand(this, sqlObjectType) : delegate.onDemand(sqlObjectType); }
@Test public void testSqlFromExternalFileWorks() throws Exception { Spiffy spiffy = SqlObjectBuilder.onDemand(dbi, Spiffy.class); ExternalSql external = SqlObjectBuilder.onDemand(dbi, ExternalSql.class); spiffy.insert(1, "Tom"); spiffy.insert(2, "Sam"); Iterator<Something> all = external.findAll(); all.next(); all.next(); assertFalse(all.hasNext()); }
@Test public void testSqlFromExternalFileWorks() throws Exception { Spiffy spiffy = SqlObjectBuilder.onDemand(dbi, Spiffy.class); ExternalSql external = SqlObjectBuilder.onDemand(dbi, ExternalSql.class); spiffy.insert(1, "Tom"); spiffy.insert(2, "Sam"); Iterator<Something> all = external.findAll(); all.next(); all.next(); assertFalse(all.hasNext()); }
@Test(expected=TransactionException.class) public void testExceptionOnClose() throws Exception { DBI dbi = new DBI(ds) { @Override public Handle open() { Handle h = EasyMock.createMock(Handle.class); h.getSqlObjectContext(); EasyMock.expectLastCall().andReturn(new SqlObjectContext()); h.setSqlObjectContext(EasyMock.anyObject(SqlObjectContext.class)); EasyMock.expectLastCall().anyTimes(); h.createStatement(EasyMock.anyString()); EasyMock.expectLastCall() .andThrow(new TransactionException("connection reset")); h.close(); EasyMock.expectLastCall() .andThrow(new UnableToCloseResourceException("already closed", null)); EasyMock.replay(h); return h; } }; Spiffy s = SqlObjectBuilder.onDemand(dbi, Spiffy.class); s.insert(1, "Tom"); }
@Test public void testIteratorClosedOnReadError() throws Exception { HandleTrackerDBI dbi = new HandleTrackerDBI(ds); Spiffy spiffy = SqlObjectBuilder.onDemand(dbi, Spiffy.class); spiffy.insert(1, "Tom"); Iterator<Something> i = spiffy.crashOnFirstRead(); try { i.next(); fail(); } catch (DBIException ex) { } assertFalse(dbi.hasOpenedHandle()); }
@Test public void testIteratorClosedIfEmpty() throws Exception { HandleTrackerDBI dbi = new HandleTrackerDBI(ds); Spiffy spiffy = SqlObjectBuilder.onDemand(dbi, Spiffy.class); ResultIterator<Something> nothing = spiffy.findAll(); assertFalse(dbi.hasOpenedHandle()); }
@Test public void testIteratorClosedIfEmpty() throws Exception { HandleTrackerDBI dbi = new HandleTrackerDBI(ds); Spiffy spiffy = SqlObjectBuilder.onDemand(dbi, Spiffy.class); ResultIterator<Something> nothing = spiffy.findAll(); assertFalse(dbi.hasOpenedHandle()); }
@Test public void testIteratorClosedOnReadError() throws Exception { HandleTrackerDBI dbi = new HandleTrackerDBI(ds); Spiffy spiffy = SqlObjectBuilder.onDemand(dbi, Spiffy.class); spiffy.insert(1, "Tom"); Iterator<Something> i = spiffy.crashOnFirstRead(); try { i.next(); fail(); } catch (DBIException ex) { } assertFalse(dbi.hasOpenedHandle()); }
@Test public void testIteratorCloseHandleOnError() throws Exception { HandleTrackerDBI dbi = new HandleTrackerDBI(ds); Spiffy s = SqlObjectBuilder.onDemand(dbi, Spiffy.class); try { s.crashNow(); fail(); } catch (DBIException e) { } assertFalse( dbi.hasOpenedHandle() ); }
@Test public void testAPIWorks() throws Exception { Spiffy s = SqlObjectBuilder.onDemand(dbi, Spiffy.class); s.insert(7, "Bill"); String bill = handle.createQuery("select name from something where id = 7").map(StringMapper.FIRST).first(); assertEquals("Bill", bill); }
@Test public void testAPIWorks() throws Exception { Spiffy s = SqlObjectBuilder.onDemand(dbi, Spiffy.class); s.insert(7, "Bill"); String bill = handle.createQuery("select name from something where id = 7").mapTo(String.class).first(); assertEquals("Bill", bill); }
@Test public void testIteratorCloseHandleOnError() throws Exception { HandleTrackerDBI dbi = new HandleTrackerDBI(ds); Spiffy s = SqlObjectBuilder.onDemand(dbi, Spiffy.class); try { s.crashNow(); fail(); } catch (DBIException e) { } assertFalse( dbi.hasOpenedHandle() ); }
@Test public void testIteratorPrepatureClose() throws Exception { HandleTrackerDBI dbi = new HandleTrackerDBI(ds); Spiffy spiffy = SqlObjectBuilder.onDemand(dbi, Spiffy.class); spiffy.insert(1, "Tom"); ResultIterator<Something> all = spiffy.findAll(); all.close(); assertFalse( dbi.hasOpenedHandle() ); }
@Test public void testIteratorPrepatureClose() throws Exception { HandleTrackerDBI dbi = new HandleTrackerDBI(ds); Spiffy spiffy = SqlObjectBuilder.onDemand(dbi, Spiffy.class); spiffy.insert(1, "Tom"); ResultIterator<Something> all = spiffy.findAll(); all.close(); assertFalse( dbi.hasOpenedHandle() ); }
@Test public void testIteratorBindsTheHandle() throws Exception { Spiffy s = SqlObjectBuilder.onDemand(dbi, Spiffy.class); s.insert(1, "Tom"); s.insert(2, "Sam"); assertNotSame(s.getHandle(), s.getHandle()); Iterator<Something> all = s.findAll(); assertSame(s.getHandle(), s.getHandle()); all.next(); assertSame(s.getHandle(), s.getHandle()); all.next(); assertFalse(all.hasNext()); assertNotSame(s.getHandle(), s.getHandle()); }
@Test public void testIteratorBindsTheHandle() throws Exception { Spiffy s = SqlObjectBuilder.onDemand(dbi, Spiffy.class); s.insert(1, "Tom"); s.insert(2, "Sam"); assertNotSame(s.getHandle(), s.getHandle()); Iterator<Something> all = s.findAll(); assertSame(s.getHandle(), s.getHandle()); all.next(); assertSame(s.getHandle(), s.getHandle()); all.next(); assertFalse(all.hasNext()); assertNotSame(s.getHandle(), s.getHandle()); }
@Test public void testTransactionBindsTheHandle() throws Exception { TransactionStuff txl = SqlObjectBuilder.onDemand(dbi, TransactionStuff.class); TransactionStuff tx2 = SqlObjectBuilder.onDemand(dbi, TransactionStuff.class); txl.insert(8, "Mike"); txl.begin(); assertSame(txl.getHandle(), txl.getHandle()); txl.updateName(8, "Miker"); assertEquals("Miker", txl.byId(8).getName()); assertEquals("Mike", tx2.byId(8).getName()); txl.commit(); assertNotSame(txl.getHandle(), txl.getHandle()); assertEquals("Miker", tx2.byId(8).getName()); }
@Test public void testTransactionBindsTheHandle() throws Exception { TransactionStuff txl = SqlObjectBuilder.onDemand(dbi, TransactionStuff.class); TransactionStuff tx2 = SqlObjectBuilder.onDemand(dbi, TransactionStuff.class); txl.insert(8, "Mike"); txl.begin(); assertSame(txl.getHandle(), txl.getHandle()); txl.updateName(8, "Miker"); assertEquals("Miker", txl.byId(8).getName()); assertEquals("Mike", tx2.byId(8).getName()); txl.commit(); assertNotSame(txl.getHandle(), txl.getHandle()); assertEquals("Miker", tx2.byId(8).getName()); }