Over a million developers have joined DZone.

4 Hands-On Approaches to Improve Your Data Access Layer Implementation

DZone's Guide to

4 Hands-On Approaches to Improve Your Data Access Layer Implementation

· Big Data Zone ·
Free Resource

How to Simplify Apache Kafka. Get eBook.

MyBatis  is a popular persistency framework with support for SQL, stored procedures and legacy database schemas (through its advanced mappings). Also, MyBatis is a mature framework, well-known and as tested in the industry as Hibernate, JPA or pureQuery.

MyBatis offers its own cache implementation, but ,if needed, it allows also to integrate your MyBatis Data Access Layer with other 3rd party libraries: Ehcache, Hazelcast, OSCache and Memcached.

The structure of the articles that will be published under the same title as the current one (as sequals) will contain :

  • Overview – in this section it will be debated why you need persistency, when it is indicated to use MyBatis, how does caching over MyBatis improves the functionality of your application. (a setup for coding environment will be presented; this is necessary to have a better understanding over caching comparison made throughout these articles).
  • Ehcache implementation over MyBatis – what aspects of the Data Access Layer functionality are improved by using it. (a coding session will be presented here)
  • Hazelcast implementation over MyBatis - what features of Hazelcast help to have a performant Data Access Layer implementation. (a coding session will be presented here)
  • OSCache implementation over MyBatis - what areas of the Data Access Layer functionality are improved by using it. (a coding session will be presented here).
  • Memcached implementation over MyBatis – why it should be used and what does improve in the Data Access Layer implementation.
  • Summary – a retrospective of the four implementations, with a comparison table of their key features and limitations.

Code for implementations can be accessed at https://github.com/ammbra/CacherPoc 


The need of persistency appeared because many applications were being developed and deployed on multi-tier environments that involved browser-based clients, web application servers and backend databases.These applications need to serve web pages on-demand by communicating to backend databases because of their dynamic nature. Persistence appeared and became part of these applications in order to achieve high scalability and performance.

Through persistency, the applications had a layer (Data Access Layer) that offered them simplified access to the database, but if the application serves web-pages on-demand with data from the database in a repeatedly manner, the application performance would be reduced. The persistency frameworks offered a built-in caching feature, but sometimes it is not enough ( the maximum size of a single value is not enough for what is retrieved from database, a different locking model is required etc).

For what applications is MyBatis indicated?

To answer this question, let's show a bit of MyBatis background. The creators of iBatis framework left the organisation they created the framework for, and because of their labor agreements, they had to continue their work on the product under a new name. The given name was MyBatis. In order to identify if you need MyBatis, please submit your current persistency solution and MyBatis to the following test:

Question Answer for MyBatis implementation (Yes or No)
Answer for currently used persistency framework  (Yes or No)
1 Is the framework part of Java Platform Standard Edition (JSE)? 
2 Is there an open-source implementation? 
3 Does the framework support invoking arbitrary SQL statements? 

4 Does the framework support batching multiple statements? 

5 Does the framework support automatic mapping of query data to Java objects? 

6 Does the framework support modifying queries independent of the Java code? 

7 Is it simple to learn?

8 Does it map directly Java objects to database tables directly (is an ORM solution)?

9 Does it offer adaptability to data model changes?

10 Is it SQL dependant?

11 Does it offer performance?

12 Does it offer portability across different relational databases?

13 Does it have community support and documentation?

Total aspects supported 

Not supported aspects

Please fill in the last column with the answers of the current persistency framework that you use, add the supported aspects and the not supported ones and compare your results. No matter which frameworks wins, it is a plus to have a proof of concept over MyBatis too. There is nothing more important then proven results.

If you go ahead with MyBatis, here are some advantages and disadvantages of it:

Advantages Disadvantages
Developers write SQL, iBATIS executes it using 
JDBC.No more try/catch/finally/try/catch (like in 
JDBC approach).
Does not have a proprietary query language.
An SQL Mapper and  support for getting rid of 
(N+1) queries.
Does not generate SQL.
Automatically maps object properties to prepared 
statement parameters; also it offers  automatically
mapping for result sets to objects.
Does not transparently persist objects.
MyBatis provides transaction management for 
database operations if no other transaction
manager is available. MyBatis can use external 
transaction management (Spring, EJB CMT, etc.) 
if available.
Does not know about object identity.

As a conclusion for our question: MyBatis is indicated when there is a need for complete control of the SQL. It is also useful when the SQL queries need to be fine-tuned. MyBatis should not be used when there is a full control over both the application and the database design, because in such cases the application could be modified to suit the database, or vice versa. In such situations, you could build a fully object-relational application, and other ORM tools are preferable.

When does the application make use of MyBatis cache?

The primary answer to this question is: when your application requests repeatedly, in a short amount of time, for large sets of data from the database. Caching is not just for “Big Data” applications; the volume of data matters and is impacting the application performance, but the need for caching appears from how often this data should be refreshed. If you are querying your database for large sets of data that never change their values, then you should not communicate with the database for each request that your browser makes. You can acquire that information from a middle layer (cache) without querying the database. Always bear in mind that database operations are time consuming and every amount of time is important for a high availability application.

Why should 3rd party libraries be used for caching?

The  3rd party libraries offer various advantages: the maximum size of a single value from cache is greater then default provided, a different locking model is offered, some allow integration with other programming languages, offer map-reduce mechanisms etc.

Setup for MyBatis Caching Proof of Concept (Cacher POC) 

  Tutorial requirements:

  • Java SE 1.6 or higher
  • An IDE
  • Maven 2.0 or higher
  • An web application server

Below is an overview of the application (each of the EJB projects is added as dependency to the war project in order to be able to inject their EJB components) :

IDE overview    and the archictectural high-level overview:

The CacherPoc project illustrates (through EJB injection of components from each of the other projects)  the behavior of what will be implemented and reviewed. Below is described the setup necessary for CacherPoc application:

  1.  Create a new Maven Web Application and name it CacherPoc
  2. Configure your pom.xml to have log4j library in place:

  3.    Inside the index.jsp file (default created with your project) you will add links to each servlet that will have injected an EJB from our sample ejb projects (EhCachePoc, HazelCastPoc, OSCachePoc and MemcachedPoc) :
<br />
<a href="EhCacheServlet?action=listEmployee" >
      Show All Employee Items with EHCache

An example of a servlet like the one referred above can be seen here:

public class EhCacheServlet extends HttpServlet {

    private static Logger logger = Logger.getLogger(EhCacheServlet.class);

    private static final String LIST_USER = "/listEmployee.jsp";

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String forward = LIST_USER;
        List<Employee> results = new ArrayList<Employee>();
        req.setAttribute("employees", results);
        RequestDispatcher view = req.getRequestDispatcher(forward);
        view.forward(req, resp);


Now everything is set for implementing each of the cache approaches. Stay tuned for the next articles on this proof of concept and learn more from: 

12 Best Practices for Modern Data Ingestion. Download White Paper.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}