Database

来源:互联网 发布:商标域名查询 编辑:程序博客网 时间:2024/05/22 13:04

Databases and Data Sources

Any data stored directly in the web server's memory (in a servlet or in a session), is volatile in that it will be lost if the web server terminates. The use of database of other data source, even directly writing to the file system, can overcome with this. In some cases, data might be stored on the web browser in the form of a cookie.


Rational Database

Relational database like MySQL and Apache Derby are common.

core syntax is common and well understood

however, embedding SQL operations in programming language is awkward

also, optimising database accesses (by database administrators) requires directly working with the SQL

therefore it is very desirable to separate the SQL operations from the domain logic

mapping objects to a relational database can be quite cumbersome.


Structural Mapping

Objects and relations handle links intwo different ways:

objects use pointers or memoryreferences to refer to other objects

    a)objects can havemultivalued fields to handle collections of other objects;

    b) objects in a collection don't need a reference back to the containing object.

relational databases handle links by forming a foreign key into another table

    a) all relation links aresingle-valued

To handle this, each object needs to maintain an identity field which is the relational identity for the object.


Inheritance

The tradeoffs are about duplication of data structure and speed-of-access.

There are three patterns to consider here:

Single table inheritance: uses one table to store all classes in a hierarchy.

                                       advantage: easy to program and modify as thehierarchy changes.

                                       disadvantage: wastes space in the database, but avoids join operations.

Concrete table inheritance: uses one table to store each concrete class a hierarchy.

                                           advantage: more tables, less wasted space.

                                            disadvantage: changes to superclassesor altering the hierarchy leads toaltering 

                                                                   all dependent tables. Avoids join operations.

Class table inheritance: uses one table to store each class in a hierarchy.

                                       advantage: the simplest relationship between the classes and tables.

                                      disadvantage: requires join operations at the database in order to pull back an object.


Relational Database Libraries

Rather than directly program for a relational database it is more convenient to use a library.

Eg. Hibernate allows annotations to POJOs (Plain Old Java Objects) that it transforms into appropriate object/relational mappings.


Object-Oriented Database

WithOO database you don't worry about the mapping.

You work with a largestructure of interconnected objects, and the database figures out when to move objects on or off disk.

Java Database is an example.

a) improve productivity: a ball-park one-third of programming effort is concerned with mapping to a relationaldatabase

b) risky: less used and not as welldeveloped as SQL


Non SQL Database

In some cases your application may not require the features of a relational databasefor the most part of its operation.

Also, it may be much more convenient for you to work with data storage formats that are identical to the formats usedfor transmitting data between client and server, such as JSONor XML.


So called "NoSQL" database use looser consistency models than traditional relational databases. They doin fact allow SQL-like languages to be used, but largely they use simple key-value stores with a goal being significant performance benefits in termsof latency and throughput.


CouchDB

Apache CouchDB is a database that uses JSON for documents, JavaScript for MapReduce queries, and regular HTTP for API.

ACouchDB database lacks a schema, or rigid pre-defined data structures such as tables.

Data stored in CouchDB is a JSON document(s).

The structure of the data or document(s) can change dynamically to accomodate evolving needs.


CouchDBBasics

A CouchDB server hosts named database, which store documents. Each document is uniquely named in the database, and CouchDB provides a RESTful (Representational state transfer) HTTP API for reading and updating (add, edit, delete) database documents.

Documents are the primary unit of data in CouchDB and consist of any number of fields and attachments.

TheCouchDB document update model is lockless and optimisitc. Document edits are made by clientapplications loading documents, applying changes,and saving them back to the database. If another clientediting the same document saves their changes first, the client get an edit conflict error on save. To resolve the update confilct, the latest document version can be opened, the latest document can be opened, the edits reapplied and the updated tried again.

Documents updates (add, edit, delete) are all or nothing, either succeeding entirely orfailing completely. The database never contains partially savedor edited documents.


CouchDB Document

A CouchDB document is a JSON object that consists of named fields. Field values may be strings, numbers, dates, or even ordered list and associative maps.


Use with JavaScript

The installed CouchDB server contains  a JavaScript library to access it.


jQuery and CouchDB

$. couch is used to communicate with a CouchDB server,  the server methods can be called directly without creating an instance.

If you simplypoint your browser to the CouchDB URL then the response is a JSON string with that information.


LightCouch for Java

LightCouch is a light-weight library for accessing CouchDB from Java.

There are many such libraries, another iscouchdb4j.

Saving a JSON document

Saving POJOsand Maps

Retrieving documents


原创粉丝点击