Databases for Mobile Devices

Document Sample
Databases for Mobile Devices Powered By Docstoc
					Databases for Mobile Devices


• Different types of data storages
• Memory management
• Local database techniques in BlackBerry
• Data Integrity in persistent storage model
• Security Techniques in Persistent storage
• Remote database techniques
• BlackBerry Gateways

         Different Types of Data
         Storage in BlackBerry
• There are two types of built-in memory in
  BlackBerry devices:
  – Flash
     • Operating system, deployed applications and all persistent
       data such as PIM data, SMS/MMS messages and emails are
       stored in flash memory persistently. The size of flash
       memory depends on the device.
  – SRAM
     • In Static RAM, data persists as long as the device is on. It is
       used in runtime process, loading applications and objects.
       It is fast.

      Extendable Memory

– microSD expandable memory card
  • You can extend the memory of your phone with a microSD
    card. MicroSD cards are persistent storages in which you
    can store documents, pictures, music and other media.

  • In order to use microSD card, the device must support
    expandable memory functionality.
  • The size of card your BlackBerry can support depends on
    the version of handheld OS software running on your

        Memory Management

• The BlackBerry JVM performs memory
  allocation, garbage collection, memory
  management and swapping data between
• Persistent data is stored in flash memory.
  When you load, execute or update application/
  data, it is copied in RAM. Then, it is written
  back to flash memory.
• Hence, data is swapped continuously between
  flash and RAM.
        Garbage Collection

• Garbage collection is handled by JVM. It
  removes unreferenced objects from RAM.
• It is only operated when:
  – JVM faces with a lack of space in RAM and can not
    allocate objects.
  – Objects handles are not available
  – The device is idle
  – The allocated heap size is exhausted

        Optimizing the memory
• As mobile devices are limited in resources
  such as memory and battery consumption, it
  is recommended to minimize the memory use
  by performing the following guidelines:
  – Help Garbage collector with release resources after
    use. Set their reference to null after use.
  – Avoid creating many objects and global variables.
    Try to reuse objects as much as possible. Having
    too many objects can cause you run out of

      Optimizing the memory
      usage (Cont.)
– Use primitive types such as int rather than objects
  like Vector, Integer
– Reduce object handles. Try to group objects. It
  reduces the number of object handles

        Database Techniques

• Databases can be accessed either locally or
• In local access, persistent storage is located
  in the mobile devices meaning that data can
  be stored locally on the device.
• In remote access, databases are located in
  the server. They can be access using data
  access tier or web services.

        Local Database
• Persistence storages techniques in
  BlackBerry devices:

  – BlackBerry Persistent Store Model
  – MIDP RMS (Record Store Model)
  – Storing data on the expanable microSD card using
    File Connection APIs
  – Relational Model
  – Access to PIM data

        BlackBerry Persistent
        Store Model
• BlackBerry Persistent Store Model provides a
  set of APIs for storing data permanently to
  flash memory in the BlackBerry device.
• Using the APIs, you are able to store, retrieve
  and manipulate data to/from the persistent
• You are also able to save an entire Java object
  to memory without doing serialization.

        BlackBerry Persistent
        Store Model (Cont.)
• The BlackBerry API for persistent store model
  is in net.rim.device.api.system package.
• In this model, data is stored as instances of
• PersistentObject can be any object that
  implements the Persistable interface.
• PersistentStore is a store of persistent

         BlackBerry Persistent
         Store Model (Cont.)
• There is not a size limit exists on a persistent
• In Week 2, you will learn more details about
  this model.

           Example- Persistent
           Storage API
//This object can be added to PersistentStore
class Student implements Persistable
   private int studentID;
   private String name;
   private String address;
   private String phone;

  public void setName(String name) { = name;
  public String getName(){
        MIDP RMS Model

• It provides a mechanism for MIDlets to
  persistently store, then retrieve and
  manipulate data.
• It is a Java ME package supported in the
  BlackBerry device as well.
• RMS library package is javax.microedition.rms
• Data is stored in a Record Store.

          MIDP RMS Model (Cont.)

• A Record Store is a collection of persistent
• A record is an array of bytes (byte[]) that can
  have a different length and type.
• Each record has a unique identifier called
  RecordID. It is automatically assigned by an
  increasing-by-one algorithm.
• The size limit for a Record Store is 64KB.

        MIDP RMS Model (Cont.)

• Each Record Store maintains a version, an
  integer number, which is incremented every
  time that the content of a record store is
• The device platform is responsible to maintain
  the integrity of the record stores throughout
  the platform operations such as reboots,
  battery change and so on.

        MIDP RMS Model (Cont.)

• All record store operations are atomic and
  synchronous. So, there will be no data
  corruption during multiple access to record
• However, if you develop multi-threads to
  access record stores, it is your responsibility
  to synchronize these accesses.

           Example- Record Store
String name="Smith";
   // To open a record store
   RecordStore rs = RecordStore.openRecordStore("phone_book",
   // To add a new record to a record store
   byte[] rec = name.getBytes();
   rs.addRecord(rec, 0, rec.length);

 }catch(RecordStoreException rse) {
          Storing Data on MicroSD

• MicroSD cards are expandable memory storages
  in which data can be stored permanently.
• In order to store /access data on the SD card, the
  Java Me package,, is
• The package specification is in JSR 75, File
  Connection API.

         Storing Data on MicroSD
• JSR 75 provides detailed information on the
  following package:
  – FileConnection API:
     • A J2ME optional package that give Java-enabled devices
       access to file systems located on mobile devices
  – PIM API:
     • A J2ME optional package that give access to PIM
       (Personal Information Management) data
• Both APIs are supported in BlackBerry
  devices as well.

         Storing Data on MicroSD
• The FileConnection API provides:
  – Interfaces:
     • FileConnection
     • FileSystemListener
  – Classes:
     • FileSystemRegistry
     • ConnectionClosedException
     • IllegalModeException

          Storing Data on MicroSD
• File Connection API provides access to files or
  directories resided on either
  – MicroSD card
     • FileConnection fc = (FileConnection)
  - Or –
  – File systems on BlackBerry devices
     • FileConnection fc = (FileConnection)

               Example- FileConnection
 public void readFile(String filename) throws IOException         {
   //Open a connection to the file
   FileConnection fc = (FileConnection)
          "file:///SDCard/BlackBerry/” + filename);
   if (fc.exists() && fc.fileSize() != 0) {
          InputStream is = fc.openInputStream();
          // Read data from the input stream
          byte[] data = new byte[256];
          int length = 0;
          while (-1 != (length =              {
                    System.out.println(“The content is ”+
                           new String(data, 0, length);
         Relational Model

• Relational model is not supported in the
  BlackBerry Persistent models either
  Persistent Store APIs or RMS.
• It is the responsibility of developers to create
  an effective relational object model.

        Access to PIM Data

• BlackBerry device Applications can access
  PIM (or PDAP) data such as contacts, tasks,
  and calendar.
• The package net.rim.blackberry.api.pdap
  supports the required interfaces and classes
  to access to the PDAP (Personal Digital
  Assistant Profile) data on the BlackBerry
• Java ME package, javax.microedition.pim,
  supports PIM data.
        Access to PIM Data
• The APIs in the BlackBerry package,
  net.rim.blackberry.api.pdap, are controlled API.
• When you use controlled APIs in your
  application, your application must be signed
  using a signature key before deploying the
  application onto the BlackBerry device.

        PIM Class

• It provides methods for accessing PIM
  databases on the BlackBerry devices.
• Invoke PIM.getInstance() to retrieve a PIM
• PIM List
  – PIM list Provides a list of PIM Items. A PIM item
    represents a collection of data for PIM data such as
    a calendar appointment, a contact, a task and so on.
  – A PIM item stores data in fields.

         PIM Class (Cont.)

• There are three types of PIM lists:
     • Represents the contact list type
     • Represents the event list type
     • Represents the ToDo list type

         PIM Class (Cont.)

• Invoke PIM. openPIMList() to open the
  specified PIM list.
  – PIMList openPIMList(int pimListType, int mode,
    String name)
     • mode indicates that the list is either READ_ONLY or

          PIM Class (Cont.)

• The next example demonstrates how to
  invoke this method.
• In order to create a contact, you must first
  create a contact list, then create contacts
  and add the information to it:
  –   Open a contact list
  –   Create a contact on a contact list
  –   Add contact information to the contact object
  –   Commit the contact object to the BlackBerry PIM
          Example- PIM Class

// Open a contact list and create a contact
import javax.microedition.pim.*;
import net.rim.blackberry.api.pdap.*;
try {
    ContactList contactList =
    (ContactList)PIM.getInstance().openPIMList (PIM.CONTACT_LIST,
    Contact contact = contactList.createContact();
} catch(Exception ex)

          Data Integrity in
          persistent storage model
• Data Integrity is one of the important features in
  persistent models.
• It means that data should have consistency in
  terms of both creating and manipulating data all
  the time.
• To maintain the data integrity, partial updates
  are not done if an error occurs during commit.

        Data Integrity in persistent
        storage model (Cont.)
• Partial updates occurs when one part of
  transaction is rolled back without rolling back
  the other parts.
• To maintain data integrity, Persistent Store
  model in BlackBerry supports both commit
  and rollback.
• Data Integrity can be compromised when JVM
  in BlackBerry devices invokes garbage
  collector due to low memory

         Security Techniques in
         Persistent storage
• Security techniques in BlackBerry devices are
  summarized as follows:
  – Security in application layer:
     • Controlled APIs:
        – Persistent Storage APIs are controlled API.
          Applications on the BlackBerry which use these APIs
          must be digitally signed by RIM.
        – You can run BlackBerry application that use controlled
          APIs in the BlackBerry simulators without signing the

       Security Techniques in
       Persistent storage (Cont.)
– Security in data layer:
   • BlackBerry core applications:
       – user data stored in BlackBerry core applications can
         be encrypted locally in flash memory by setting IT
         policy by administrator.

   • Third party applications:
      – They should register their data with this encryption
         service using cryptography APIs. So, before a third
         party application data commits to flash memory, the
         encryption service encrypts it with the same key

         Remote database
• You can access remote data sources from the
  BlackBerry devices.

• The following presents different ways to
  access remote databases :
  – N-tier applications
  – MDS Applications
     • Web Services

         Remote database
         techniques (Cont.)
• In order to access data remotely, a distributed
  (n-tier) architecture is required.

• There are several ways to implement a n-tier

• In the following slide, a 4-tier architecture is

        4-Tier Architecture

• A 4-tier architecture provides 4 separate layers
  for each of the following tiers:
  – Client-tier: mobile devices are considered as client-
  – Server-tier: known as business tier. Business
    objects, rules and data transformation can be
    designed in this tier.
  – Data access tier: Interfaces to access data storages,
    handling data I/O are designed in this layer.
  – Data-tier: Databases and data storages are in this
                     4-Tier Architecture

          Wireless          Connection


Client Tier           Business Tier                 Data Access Tier   Databases

         Remote Access to Data
• The Server tier in this architecture can be:
  – An application server :
     • It is a server-side application deployed into the server. It
       provides the business logic, handles requests, processes
       and send the response back to the client.
  – A Web Service
     • It implements SOA (Service-Oriented Architecture)
       concepts. A client-tier application invokes the web service
       interfaces, in order to access the Web Service methods.

              Remote Access to Data
              Sources (Cont.)


                     Web Service

Client-tier           Server-tier                 Database
        BlackBerry Gateways
• BlackBerry applications can use HTTP, HTTPS
  or TCP socket to make a connection over the
  wireless network.
• In order to minimize the network connection
  issues, it is recommended to use one of the
  three wireless gateways to proxy the
  connection to the Internet or Intranet.

        BlackBerry Gateways
• In addition to reduce the network connection
  issues, they let applications have a
  consistent connectivity.
• The Gateways are:
  – BlackBerry Enterprise Server as an Intranet
  – BlackBerry Internet Service as an Internet gateway
  – Wireless Service Provider’s Internet gateway

        BlackBerry Enterprise
        Server (BES) Gateway
• BES is a link between BlackBerry devices,
  wireless network, and application servers
  enabling access from BlackBerry devices to
  the corporate data resided on the corporate
• The next slide describes the architecture.

Remote Data Access using
BES with MDS service


        MDS Applications

• The BlackBerry Mobile Data System (MDS) is
  a framework which provides tools to build and
  deploy BlackBerry Enterprise applications.
• Before, in order to use corporate applications,
  customers have to be tied to the corporate
  email server.
• Now, BES for MDS has separated wireless
  applications from email.

       MDS Applications (Cont.)

• It enables mobile access to applications only.
• In other words, BES for MDS applications
  provides you a choice to deploy both
  wireless email and applications or wireless
  applications only.

          MDS Applications (Cont.)
• BES for MDS applications offers the following
  – Security:
     • IT Policy: It controls the application accesses.
     • End-to-end authenticated connection: It encrypts all data.
  – Push service to provide real-time data
  – Centralized administration console
  – Deploy over-the-air (OTA)

        BlackBerry Internet
        Service (BIS) Gateway
• BIS is an Internet and email service for
  BlackBerry devices
• Allows to automatic delivery of email
  messages, and convenient access to Internet
• This service uses RIM push technology
  allowing real-time access of email.