onDemand will obtain and release connection automatically, as it needs to. Generally this means that it will obtain a connection to execute a statement and then immediately release it, but various things such as open transactions or iterator based results will lead to the connection remaining open until either the transaction completes or the iterated result is fully traversed. So even if when two requests are accessing the same resource, they will be in different handle. So it wont cause any problem.
public abstract class Dao implements GetHandle {
public void printHandle() {
System.out.println(getHandle());
}
}
@Test
public void testHandle() {
Dao onDemandDao = dbi.onDemand(Dao.class);
Handle handle = dbi.open();
Dao handleAttachedDao = handle.attach(Dao.class);
Dao openDao = dbi.open(Dao.class);
for(int i=0; i< 5; i++ ) {
onDemandDao.printHandle();
}
for(int i=0; i< 5; i++ ) {
handleAttachedDao.printHandle();
}
for(int i=0; i< 5; i++ ) {
openDao.printHandle();
}
}
The output for this test is,
org.skife.jdbi.v2.BasicHandle@35d114f4
org.skife.jdbi.v2.BasicHandle@3684d2c0
org.skife.jdbi.v2.BasicHandle@4be460e5
org.skife.jdbi.v2.BasicHandle@454e9d65
org.skife.jdbi.v2.BasicHandle@7805478c
org.skife.jdbi.v2.BasicHandle@6807989e
org.skife.jdbi.v2.BasicHandle@6807989e
org.skife.jdbi.v2.BasicHandle@6807989e
org.skife.jdbi.v2.BasicHandle@6807989e
org.skife.jdbi.v2.BasicHandle@6807989e
org.skife.jdbi.v2.BasicHandle@c2e33
org.skife.jdbi.v2.BasicHandle@c2e33
org.skife.jdbi.v2.BasicHandle@c2e33
org.skife.jdbi.v2.BasicHandle@c2e33
org.skife.jdbi.v2.BasicHandle@c2e33
You can see, onDemand Dao everytime creates new handle when access the method.