I am searching to produce a sample project while learning Guice which utilizes JDBC to seeOrcreate to some SQL database. However, after many years of utilizing Spring and allowing it to abstract away connection handling and transactions I am battling to operate it our conceptually.

Let me possess a service which begins and stops a transaction and calls numerous databases which reuse exactly the same connection and take part in exactly the same transaction. My questions are:

  • Where will i create my Datasource?
  • How do you provide the databases use of the bond? (ThreadLocal?)
  • Easiest way to handle the transaction (Creating an Interceptor to have an annotation?)

The code below shows the way i would do that in Spring. The JdbcOperations injected into each repository would have the connection connected using the active transaction.

I've not been capable of finding many lessons that go over this, beyond ones which show creating interceptors for transactions.

I'm pleased with ongoing to make use of Spring because it is working perfectly during my projects, but Let me understand how to do that in pure Guice and JBBC (No JPA/Hibernate/Warp/Re-using Spring)

@Service
public class MyService implements MyInterface {

  @Autowired
  private RepositoryA repositoryA;
  @Autowired
  private RepositoryB repositoryB;
  @Autowired
  private RepositoryC repositoryC; 

  @Override
  @Transactional
  public void doSomeWork() {
    this.repositoryA.someInsert();
    this.repositoryB.someUpdate();
    this.repositoryC.someSelect();  
  }    
}

@Repository
public class MyRepositoryA implements RepositoryA {

  @Autowired
  private JdbcOperations jdbcOperations;

  @Override
  public void someInsert() {
    //use jdbcOperations to perform an insert
  }
}

@Repository
public class MyRepositoryB implements RepositoryB {

  @Autowired
  private JdbcOperations jdbcOperations;

  @Override
  public void someUpdate() {
    //use jdbcOperations to perform an update
  }
}

@Repository
public class MyRepositoryC implements RepositoryC {

  @Autowired
  private JdbcOperations jdbcOperations;

  @Override
  public String someSelect() {
    //use jdbcOperations to perform a select and use a RowMapper to produce results
    return "select result";
  }
}