            ------
            Using Mirage with other Frameworks
            ------
            Naoki Takezoe
            ------
            2010-05-02

Using Mirage with other Frameworks

 You can use Mirage with other frameworks such as Spring Framework or Google Guice.

* {Spring Framework}

 Mirage can work with {{{http://www.springsource.org/}Spring Framework}} using
 <<{{{apidocs/jp/sf/amateras/mirage/integration/spring/SpringConnectionProvider.html}SpringConnectionProvider}}>>
 instead of DefaultConnectionProvider.

--------
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="driverClassName" value="org.h2.Driver" />
  <property name="url" value="jdbc:h2:tcp://localhost:9092/test" />
  <property name="username" value="sa" />
  <property name="password" value="" />
</bean>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource"/>
</bean>

<bean id="connectionProvider" class="jp.sf.amateras.mirage.integration.spring.SpringConnectionProvider">
  <property name="transactionManager" ref="transactionManager" />
</bean>

<bean id="connectionProvider" class="jp.sf.amateras.mirage.dialect.HyperSQLDialect">
  <property name="dialect" ref="dialect" />
</bean>

<bean id="sqlManager" class="jp.sf.amateras.mirage.SqlManagerImpl">
  <property name="connectionProvider" ref="connectionProvider" />
  <property name="dialect" ref="dialect" />
</bean>
--------

 You can use SqlManager as follow in your code:

--------
private SqlManager sqlManager;


public void setSqlManager(SqlManager sqlManager){
  this.sqlManager = sqlManager;
}

@Transactional
public void execute(){
  // Database access using SqlManager
  ...
}
--------

 In Spring Framework, JDBC connection information are defined in Spring bean definition file, and transaction
 is controlled by AOP. So you don't need to make jdbc.properties and don't need to use Session and SessionFactory.

* {Google Guice}

 You can use Mirage with {{{http://code.google.com/p/google-guice/}Google Guice}} using
 <<{{{apidocs/jp/sf/amateras/mirage/integration/guice/MirageModule.html}MirageModule}}>>.

--------
Injector injector = Guice.createInjector(new MirageModule());

Session session = injector.getInstance(Session.class);
SqlManager sqlManager = injector.getInstance(SqlManager.class);

session.begin();

try {
  // database access using SqlManager
  ...
  session.commit();

} catch(Exception ex){
  session.rollback();
  throw ex;
} finally {
  session.release();
}
--------

 MirageModule supports @Transactional for automatic transaction control.
 It applies TransactionInterceptor to methods of managed components.
 <<{{{apidocs/jp/sf/amateras/mirage/integration/guice/TransactionInterceptor.html}TransactionInterceptor}}>>
 begins and commits / rollbacks a transaction automatically around the applied method.
 Of course, you can use OpenSessionInViewFilter instead of
 <<{{{apidocs/jp/sf/amateras/mirage/integration/guice/Transactional.html}@Transactional}}>> in the web application.

--------
public class EmployeeDao {

  @Inject
  private SqlManager sqlManager;

  @Transactional
  public void insertEmployee(Employee employee){
    sqlManager.insertEntity(employee);
  }

}

Injector injector = Guice.createInjector(new MirageModule());
EmployeeDao dao = injector.getInstance(EmployeeDao.class);

Employee employee = new Employee();
employee.empId = 1;
employee.name = "Naoki Takezoe";

dao.insertEmployee(employee);
--------

* {Seasar2}

 {{{http://s2container.seasar.org/2.4/en/}Seasar2}} is a DI container which is used in Japan.
 Seasar2 already has a O/R mapper named S2JDBC.
 S2JDBC is similar to Mirage because we referred S2JDBC in various respects to design Mirage.
 However you can use Mirage instead of S2JDBC.

 Register <<SqlManager>> and <<{{{apidocs/jp/sf/amateras/mirage/integration/seasar/SeasarConnectionProvider.html}SeasarConnectionProvider}}>> into your dicon file as follows:

--------
<component class="jp.sf.amateras.mirage.integration.seasar.SeasarConnectionProvider" />
<component class="jp.sf.amateras.mirage.SqlManagerImpl" />
--------

 You can use SqlManager as follow in your code:

--------
@Resource
protected SqlManager sqlManager;

public void execute(){
  // Database access using SqlManager
  ...
}
--------

