Download (Data access technology) in Spring

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
PureQuery Integration with Spring
D AT A
ACCESS USING
PURED AT AQUERY
IN
SPRING
Version 0.2
By: Ambrish Bhargava
1
PureQuery Integration with Spring
Table of Content
1
IBM PDQ (Data access technology): .................................................................................. 4
1.1
Support for PDQ Inline-Style:...................................................................................... 4
1.1.1
Setting PDQ Inline-style in the Spring environment: ............................................ 4
1.1.2
Using PDQTemplate and PDQDaoSupport ......................................................... 5
1.2
Support for PDQ Annotated-Style: .............................................................................. 6
1.2.1
Setting PDQ Annotated-style in the Spring environment: .................................... 6
2
PureQuery Integration with Spring
Revision History
Version
0.1
0.2
Date
30/08/07
06/09/07
Authors
Ambrish Bhargava
Mario Ds Briggs
Remarks
Initial write up
Changes and comments.
3
PureQuery Integration with Spring
1 IBM PDQ (Data access technology) in Spring:
PDQ support in the Spring framework much resembles the JDBC / iBatis / Hibernate support
(within Spring) in that it supports the same template style programming and just as with JDBC or
Hibernate or iBatis, the PDQ support works with Spring's exception hierarchy and let's you enjoy
the all IoC and resource management ( DB connections) features Spring has.
Transaction management can be handled through Spring's standard facilities. There are no
special transaction strategies for PDQ, as there is no special transactional resource involved
other than a JDBC Connection/DataSource. Hence, Spring's standard JDBC
DataSourceTransactionManager or JtaTransactionManager are perfectly sufficient to achieve
Declarative and Programmatic Transaction management.
There are two ways to access a database using PDQ and they are:
1
2
Inline-style.
Annotated-style (Also known as method-style).
All the features mentioned above are supported with both the Inline and Annotated style.
1.1 Support for PDQ Inline-Style:
1.1.1 Setting-up PDQ Inline-style in the Spring environment:
If we want to use the inline-style of PDQ within Spring framework, then we can use the
PdqTemplate in the Spring Container. The DataSource needs to be specified on the
PdqTemplate.This It can be done via a XML file as follows. The option of directly initialization
also exists.
Example applicationContext.xml:
<beans>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.username}" />
<property name="password" value="${database.password}" />
</bean>
<bean id="pdqTemplate"
class="org.springmodules.purequery.PdqTemplate">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
</beans>
4
PureQuery Integration with Spring
The PdqTemplate will obtain database connections and release them correctly as and when
required thus preventing database connection leaks and freeing the developer from this tedious
and error prone task.
1.1.2 Using PdqTemplate and PdqDaoSupport
The usage is very much similar to Spring’s JdbcTemplate. PqdDaoSupport offers a supporting
class. We extend it to implement our DAO :
Example AccountDao.java:
package example;
import org.springmodules.purequery.support.PdqDaoSupport;
public class AccountDao extends PdqDaoSupport {
/*
* Developer will write code here
*/
public Account[] getAccounts() {
String sql = "SELECT * FROM ACCOUNT";
return (Account[]) getPdqTemplate().queryArray(sql,
Account.class);
}
// Other methods goes here
}
The following bean is required (to be added in applicationContext.xml as shown earlier) to
initialize the PdqTemplate.
<bean id="accountDao" class="example.AccountDao">
<property name="template">
<ref bean="pdqTemplate" />
</property>
</bean>
5
PureQuery Integration with Spring
1.2 Support for PDQ Annotated-Style:
1.2.1 Setting-up
PDQ
environment:
Annotated-style
in
the
Spring
In PDQ (without Spring framework support), using the annotated method style involves the
following steps
1
Create an annotated interface either by hand or use the tooling to auto-generate the
annotated interface from the database/bean/SQL.
2
Once the annotated interface is in place, the tooling will auto-generate an
implementation of this interface as <InterfaceName>Impl.java.
3
Instantiate an instance of the implementation using the DataFactory.getData(Class
interfaceClazz, …) methods.
Example AccountData.java:
import java.util.Iterator;
import com.ibm.pdq.annotation.Select;
public interface AccountData {
@Select(sql = "select * from ACCOUNT")
Iterator<Account> getAccountsInIterator();
// Other queries goes here.
}
Hand-crafted or PDQ Tool-Generated annotated interface.
The following Implementation (AccountDataImpl.java) file is generated corresponding to above
interface (AccountData.java) automatically using PDQ GeneratorTool.
Example AccountDataImpl.java:
package example;
import java.sql.Types;
import java.util.Iterator;
import com.ibm.pdq.annotation.Metadata;
import com.ibm.pdq.runtime.generator.BaseData;
import com.ibm.pdq.runtime.handlers.RowHandler;
6
PureQuery Integration with Spring
import com.ibm.pdq.runtime.statement.SqlStatementType;
import com.ibm.pdq.runtime.statement.StatementDescriptor;
public class AccountDataImpl extends BaseData implements AccountData {
public AccountDataImpl() {
}
public static final String generatorVersion = "1.0.18";
public static final String identifier = "Account";
public static final String collection = "NULLID";
public static final long generationTime = 1189081472843l;
// select * from ACCOUNT
public Iterator<Account> getAccountsInIterator() {
return queryIterator(getAccountsInIteratorStatementDescriptor);
}
@Metadata()
public static final StatementDescriptor
getAccountsInIteratorStatementDescriptor =
createStatementDescriptor(
"getAccountsInIterator()",
"select * from ACCOUNT",
SqlStatementType.QUERY,
null, null, null,
new GetAccountsInIteratorRowHandler(),
new int[][] {
{ Types.INTEGER, Types.VARCHAR },
{ 10, 10 },
{ 0, 0 },
{ 0, 1252 }
},
identifier, collection, generationTime, 1
);
public static class GetAccountsInIteratorRowHandler implements
RowHandler<Account> {
public Account handle(java.sql.ResultSet rs,
Account returnObject) throws java.sql.SQLException {
returnObject = new Account();
returnObject.setId(rs.getInt(1));
returnObject.setName(rs.getString(2));
return returnObject;
}
}
}
PDQ Tool Generated implementation for annotated interface.
7
PureQuery Integration with Spring
If we want to use the annotated method style of PDQ within Spring framework, Step1 and Step2
remain the same as above. For Step 3, instead of using the DataFactory, we use the
PdqAnnotatedMethodFactory. The DataSource and annotated interface needs to be specified
on the PdqAnnotatedMethodFactory. This It can be done via a XML file as follows.
Example applicationContext.xml:
<beans>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.username}" />
<property name="password" value="${database.password}" />
</bean>
<bean id=”accountDaoInterface” class=”java.lang.Class”
factory-method=”forName”>
<constructor-arg value=”example.AccountDao” />
</bean>
<bean id="accountDao"
class="org.springmodules.purequery.PdqAnnotatedMethodFactory"
factory-method=”getData”>
<constructor-arg>
<ref bean=" accountDaoInterface" />
<ref bean=”datasource” />
</constructor-arg>
</bean>
</beans>
PDQ annotated style support in Spring also provides automatic connection management similar
to inline style support.
8