            ------
            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="sqlManager" class="jp.sf.amateras.mirage.SqlManagerImpl">
  <property name="connectionProvider" ref="connectionProvider" />
</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 informations are defined in Spring bean desfinition 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 initializes Mirage using jdbc.properties and not provides advanced features such as connection pooling
 and automatic transaction control.

 So you might customize MirageModule to use DBCP. And also you might have to use OpenSessionInViewFilter with MirageModule,
 or create your own interceotor to control transaction.