raw JDBC, Guice and Transactions -
i try create service multiple dao's addressdao , customerdao, know want create transaction spans two, like:
@inject private customerdao customerdao; @inject private addressdao addressdao; customer getcustomer(int id) { customer customer = customerdao.getcustomer(id); customer.setaddress(addressdao.getaddress(customer.getaddressid()); return customer; }
inside daos stuff lookin that
public class customerdaojdbcimpl implements customerdao { private static final logger logger = loggerfactory.getlogger(customerdaojdbcimpl.class); @inject private database db; public customer getcustomer(int id) { try(connection connection = db.getconnection()) { } catch(sqlexception e) { ... } } }
now since connection gets injected dao's can't span transaction. think don't right , maybe need book understand everything.
what preferred solutions? or doing wrong?
currently think have solution made up, lacks threading.
i got code from: https://stackoverflow.com/a/2353795/2250209
and here: https://github.com/mybatis/guice/tree/master/src/main/java/org/mybatis/guice/transactional
currently have database class pulls connection datasource, class gets injected dao, , if annotated dao or service connection kept open until call commit or rollback, don't know if best pattern since people recommend close connection inside method.
Comments
Post a Comment