2. Data Access
• Data access pattern
• Data Sources / Connection pool
• Data access with JDBC Template
• myBatis
• Object Relational Mapping (ORM) Data Access
4. • Data Access Object Pattern or DAO pattern is used to separate low level data
accessing API or operations from high level business services. Following are the
participants in Data Access Object Pattern.
• Data Access Object Interface - This interface defines the standard operations to
be performed on a model object(s).
• Data Access Object concrete class - This class implements above interface. This
class is responsible to get data from a data source which can be database / xml or
any other storage mechanism.
• Model Object or Value Object - This object is simple POJO containing get/set
methods to store data retrieved using DAO class
8. We used the sample application provided in section "Connecting to a Data Source" to open a connection to Oracle9i using
a DataDirect Connect for JDBC 3.2 Oracle driver. We closed the connection at 100, 100, 1000, and 3000 iterations. We
ran this sample application on a single Pentium IV 2 GHz machine with 512 MB RAM connected to an Oracle9i Server
(Pentium IV 2 GHz machine with 512 MB RAM). The total elapsed time for each run was measured when connection
pooling was used and was measured again when connection pooling was not used as shown in the following table:
100 Iterations 100 Iterations 1000 Iterations 3000 Iterations
Pooling 547 ms <10 ms 47 ms 31 ms
Non-Pooling 4859 ms 4453 ms 43625 ms 134375 ms
JDBC CONNECTION POOLING - PERFORMANCE BENCHMARKS
10. Action Spring You
Define connection parameters. X
Open the connection. X
Specify the SQL statement. X
Declare parameters and provide parameter values X
Prepare and execute the statement. X
Set up the loop to iterate through the results (if any). X
Do the work for each iteration. X
Process any exception. X
Handle transactions. X
Close the connection, statement and resultset. X
Spring JDBC - who does what?
11. Querying (SELECT)
Here is a simple query for getting the number of rows in a relation:
int rowCount = this.jdbcTemplate.queryForObject("select count(*) from t_actor", Integer.class);
A simple query using a bind variable:
int countOfActorsNamedJoe = this.jdbcTemplate.queryForObject(
"select count(*) from t_actor where first_name = ?", Integer.class, "Joe");
Querying for a String:
String lastName = this.jdbcTemplate.queryForObject(
"select last_name from t_actor where id = ?", new Object[]{1212L}, String.class);
Querying and populating a single domain object:
Actor actor = this.jdbcTemplate.queryForObject(
"select first_name, last_name from t_actor where id = ?",
new Object[]{1212L},
new RowMapper<Actor>() {
public Actor mapRow(ResultSet rs, int rowNum) throws SQLException {
Actor actor = new Actor();
actor.setFirstName(rs.getString("first_name"));
actor.setLastName(rs.getString("last_name"));
return actor;
}
});
12. Updating (INSERT/UPDATE/DELETE) with jdbcTemplate
this.jdbcTemplate.update("insert into t_actor (first_name, last_name) values (?, ?)", "Leonor", "Watling");
this.jdbcTemplate.update( "update t_actor set last_name = ? where id = ?","Banjo", 5276L);
this.jdbcTemplate.update("delete from actor where id = ?",Long.valueOf(actorId));
13. JdbcTemplate best practices
public class JdbcCorporateEventDao implements CorporateEventDao {
private JdbcTemplate jdbcTemplate;
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
// JDBC-backed implementations of the methods on the
CorporateEventDao follow...
}
14. Using @Repository
@Repository
public class JdbcCorporateEventDao implements CorporateEventDao {
private JdbcTemplate jdbcTemplate;
@Autowired
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
// JDBC-backed implementations of the methods on the
CorporateEventDao follow...
}
15. myBatis
• iBATIS is a persistence framework which automates the mapping between SQL databases and objects in
Java, .NET, and Ruby on Rails. In Java, the objects are POJOs (Plain Old Java Objects). The mappings are
decoupled from the application logic by packaging the SQL statements in XML configuration files. The result is a
significant reduction in the amount of code that a developer needs to access a relational database using lower
level APIs like JDBC and ODBC.
• Other persistence frameworks such as Hibernate allow the creation of an object model (in Java, say) by the user,
and create and maintain the relational database automatically. iBATIS takes the reverse approach: the developer
starts with a SQL database and iBATIS automates the creation of the Java objects. Both approaches have
advantages, and iBATIS is a good choice when the developer does not have full control over the SQL database
schema. For example, an application may need to access an existing SQL database used by other software, or
access a new database whose schema is not fully under the application developer's control, such as when a
specialized database design team has created the schema and carefully optimized it for high performance.
• On May 21, 2010 the development team forked the code creating a new project called MyBatis and making new
releases there. As a consequence the Apache iBATIS project became inactive and was moved to the Apache
Attic in June 2010.
16. Object-relational mapping
• Compared to traditional techniques of exchange between
an object-oriented language and a relational database,
ORM often reduces the amount of code that needs to be
written.
• Disadvantages of ORM tools generally stem from the high
level of abstraction obscuring what is actually happening
in the implementation code. Also, heavy reliance on ORM
software has been cited as a major factor in producing
poorly designed databases.
17. Comparison of object-relational mapping software
Software Platform Availability License Version Persistence Specification
Doctrine PHP Open source MIT 2.4/April 8, 2014
DataNucleus
Java Virtual
Machine
Open source Apache License 2 4.1.0.RELEASE / May 19, 2015 JDO
RedBeanPHP PHP Open source BSD License 4/April 1, 2014
Dapper .NET 4.0 Open source Apache License 2.0 1.8 NuGet
ECO .NET 4.0 Commercial ECO 6 Final (2011-04-18[1])
EntitySpaces .NET 4.0 Open source Modified BSD License 2012.1.0930.0 / October 4, 2012
EclipseLink
Java Virtual
Machine
Open source
Eclipse Public License Version 1.0 (EPL) and Eclipse Distribution
License Version 1.0 (EDL)
2.4.2 / July 4, 2013 JPA 2.0
Hibernate
Java Virtual
Machine
Open source GNU Lesser General Public License 4.2.5 / August 28, 2013 JPA 2.0
MyBatis/iBATIS Cross-platform Open source Apache License 2.0
jOOQ
Java Virtual
Machine
Open source Apache License 2.0 and Proprietary License 3.2.0 / October 9, 2013
Apache Cayenne
Java Virtual
Machine
Open source Apache License 2.0 3.0.2 / July 21, 2011
Microsoft ADO.NET
Entity Framework
.NET 4.5 Part of .NET 4.5 Apache License 2.0[2] v6.0 (2014)
nHibernate .NET 4.5 Open source GNU Lesser General Public License 4.0 (2014-08-17[3])
ODB
Cross-platform
C++
Dual-licensed GNU General Public License and Proprietary License 2.3.0 / October 30, 2013
SQLAlchemy Python Open source MIT License 1.0.9 / October 20, 2015
SQLObject Python Open source LGPL 2.1.2 / March 15, 2015
Storm Python Open source LGPL 2.1 0.19 / October 5, 2011
SubSonic .NET 2.0 Open source New BSD 3.0 / July 2009
TopLink
Java Virtual
Machine
Commercial Oracle License 10g JPA
Skipper PHP Commercial Proprietary software 3.0
WebORB Integration
Server
.NET, Java,
PHP
Commercial & Open
source
WebORB for .NET and WebORB for Java = Proprietary License,
WebORB for PHP = Mozilla Public License
WebORB for .NET v.4.2 (Oct 2010), WebORB for Java
v.4.0 (Sep 2010), WebORB for PHP (Sep 2008)
DBIx::Class Perl Open source Artistic License 1.0 & GPL 0.082820 / March 20, 2015[4]