Database¶
Items
Programming Techniques¶
Here, a database programming method by using S2JDBC is explained.S2JDBC is O/R Mapper of Seasar2.For details of S2JDBC, refer to http://s2container.seasar.org/2.4/ja/s2jdbc_abstract.html.
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.htmlRefer 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(); } }
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 backwithout 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"; } } } }