intra-mart Accel Platform / Sastruts SAStruts+S2JDBC Programming Guide

«  To use the user time zone, date and time format   ::   Contents   ::   Log  »

Database

Programming Techniques

Here, a database programming method by using S2JDBC is explained.
S2JDBC is O/R Mapper of Seasar2.

Create an Entity

Entity source code is auto-generated from table definition in database by using S2JDBC-Gen.
For usage methods of S2JDBC-Gen, refer to http://s2container.seasar.org/2.4/ja/s2jdbc_gen/setup.html
Refer to the execution of Ant task.

Create a Service

Service is a class which stores the operations for entity. Business logic is defined in entity or service.
While creating a service, specify org.seasar.extension.jdbc.service.S2AbstractService in parent class.
For example, following is a service for an entity “Sample”.
public SampleService extends S2AbstractService<Sample> {
    ...
}

Acquisition of Records Using Service Class

In case of acquiring all the records of Sample table by using a service in Action class, implement as shown below.
public class SimpleAction { 
    /**
     * Service
     */
    @Resource
    protected SampleService sampleService;

    /**
     * Data array in sample table
     */
    public List<Sample> records;

    @Execute(validator = false)
    public String index() {
        records = sampleService.findAll();
        return "index.jsp";
    }
}
Auto-binding of SampleService is done by creating @Resource annotation.
Therefore, creating an instance of service class is not required.
Entire data of Sample table can be acquired by using SampleService#findAll() method.

To Acquire Records Identical to the Specified Conditions

In case of acquiring the records identical to the specified conditions, access the database by using JdbcManager.
For example, to acquire id of a record of “Sample01” from Sample table, implement as shown below.
public SampleService extends S2AbstractService<Sample> {
    /**
     * Acquire id of a record of "Sample01" data from Sample table.
     */
    public Sample getSample01() {
        return jdbcManager.from(Sample.class).where("id = ?", "sample01").getSingleResult();
    }
}

Use Shared Database

Setting as shown below is required to connect with the shared database.
  1. Carry out connection settings of data source which connects as shared database for each Web Application Server.
  2. Carry out the shared database settings of intra-mart Accel Platform by specifying arbitrary connection ID.
  3. Include dicon file which has set the shared database.
  4. Create a service for connecting the shared database.

Note

Here, explain only the setup method of S2JDBC.
Refer to setup guide for database setup method and shared database setup method.
For example, when connecting to the shared database which is set as connection ID “Sample”, setup/implement as shown below.
  1. Create “shared-jdbc.dicon” by copying “jdbc.dicon” and setup the shared database as shown below.
    <components namespace="jdbc">
        <component  name="sharedDataSource" class="jp.co.intra_mart.framework.extension.seasar.util.SharedDataSource">
            <arg>"sample"</arg>
        </component>
    </components>
    
    Specify the connection ID of shared destination database in arg.
  2. Create “s2shared-jdbc.dicon” by copying “s2jdbc.dicon” and set JdbcManager for shared database as shown below.
    <components>
    	<include path="shared-jdbc.dicon"/>
    	<component name="sharedJdbcManager" class="org.seasar.extension.jdbc.manager.JdbcManagerImpl">
    </components>
    
  3. Create a service class for connecting with shared database and specify JdbcManager at @Resource.
    public abstract AbstractSharedDatabaseService<T> extends S2AbstractService<T> {
        @Resource(name="sharedJdbcManager")
        public void setJdbcManager(JdbcManager jdbcManager) {
            this.jdbcManager = jdbcManager;
        }
    }
    

Transaction

Transaction control settings are carried out by customizer.dicon. For the transaction control settings of standard intra-mart Accel Platform,
TxAttributeCustomizer is set for the action, logic and service methods.
If this setting is used, the method will start only if transaction does not start before the method.
If the executed method closes normally, it will be committed and if exception is thrown, it will be rolled back.

Rollback Using UserTransaction#setRollbackOnly()

Many a times a transaction needs to be rolled back without throwing an exception.
In such case, by using UserTransaction#setRollbackOnly(), the transaction can be rolled back
without throwing an exception.
public SampleAction {
    @Resource
    @ActionForm
    public SampleForm sampleForm;

    @Resource
    protected SampleService sampleService;

    @Resource
    protected UserTransaction userTransaction;

    /**
     * Register the input contents of form.
     */
    @Execute(validator = true)
    public String add() { 
        Sample entity = new Sample();
        // Copy the input contents of form in entity.
        BeanUtil.copyProperties(sampleForm, entity);
        try {
            // Registration process
            sampleService.insert(entity);
        } catch (Exception e) {
            // Rollback the transaction
            try {
                userTransaction.setRollbackOnly(); 
            } catch (Exception e) {
                throw  new  SystemRuntimeException(e);
            }
        }
        return "index.jsp";
    }

}

Complete Manual Control of Transactions Using UserTransaction Object

Start of the commit and rollback of a transaction can be manually controlled by using UserTransaction object.
During manual control of transaction, if a transaction is not done using TransactionAttributeType.NEVER,
carry out the transaction control which has used UserTransaction object.
public SampleAction {
    @Resource
    @ActionForm
    public SampleForm sampleForm;

    @Resource
    protected SampleService sampleService;

    @Resource
    protected UserTransaction userTransaction;

    /**
     * Register the input contents of form.
     */
    @Execute(validator = true)
    @TransactionAttribute(TransactionAttributeType.NEVER)
    public String add() { 
        Sample entity = new Sample();
        // Copy the input contents of form in entity.
        BeanUtil.copyProperties(sampleForm, entity);
        
        // Start transaction
        userTransaction.begin();
        try {
            // Registration process
            sampleService.insert(entity);
        } catch (Exception e) {
            //  Reserve "Rollback" to rollback the transaction with finally clause.
            userTransaction.setRollbackOnly(); 
        } finally {
            if (userTransaction.getStatus() == Status.STATUS_ACTIVE) {
                // Commit
                userTransaction.commit();
                return "index.jsp";
            } else {
                // Rollback
                userTransaction.rollback();
                return "error.jsp";
            }
        }
    }
}

«  To use the user time zone, date and time format   ::   Contents   ::   Log  »