Posty

Wyświetlam posty z etykietą Java

Spring Data 2# - support for Couchbase

In this article we will have a look at how NoSQL Couchbase database is supported in Spring, especialy in Spring Data module. Couchbase is a document oriented NoSQL database which has gained a lot of popularity in the last few years. The main reason for that may be its specialization in a low latency data management for large scale web applications. Couchbase introduction Couchbase is a NoSQL document database which is a subset of a key-value stores. It is designed to store and retrieve data in JSON format. Each separate JSON item stored in the database is called a document. Couchbase does not force any specific schema on the data that is stored. It is often said that in NoSQL databases any schema specific constraints are moved from the database layer to the application layer . It often provides a better ways to develop a software with dynamic data structure changes since changes in the documents structure do not break any database constraints or schema. In this case you can up...

Hibernate 3# - Session API

Hibernate's Session is the interface that provides API for Java application to interact with persistence layer. It introduces CRUD methods to operate on entities. It also contains methods used to handle transactions. We can say, that Session  is the core interface in the Hibernate library. We will have a look at the core features of Session and how it is handled in Spring framework. Entity Management You will often see additional layers of abstraction over Session interface (like in Spring, which is discussed later). However, in this part of the article I will show some basic examples of using  Session object directly with regard to operations on entities. Entity is a domain model class. Its instance represents in the Java world a database row. As an entity example we will use following class: As you can see we are using multiple annotations which are needed by Hibernate to properly map the entity instance to a database row and vice versa. This annotations ...

Hibernate 2# - physical vs logical transactions

There are often some confusions in the regard of using 'transaction' term, especially when talking about ORM solutions like Hibernate. Sometimes transaction can have a different meaning depending on the context. It needs to be understood in order to be able to prevent any data anomalies during web development. We will have a look on what are the differences between physical and logical transactions.  Physical transaction Physical transaction is the actual transaction that takes place on the database level. It means that the beginning of the physical transaction is when actual transaction on the database level is started. The end of the physical transaction takes place when this transaction is commited or rollbacked on the database level. Atomicity of operations made during physical transaction is guarded by database itself. Logical transaction Logical transactions on the other hand are those handled on the the application side, for example Spring application. Logica...

Hibernate 1# - entity states overview

Obraz
Hibernate  is the most common choice when it comes to the ORM libraries used in Java backend projects. Being an ORM library Hibernate uses entities rather than SQL statements to manage database changes. In this article we will have a look at all the possible states of entities. This knowledge will give you awareness of what actions will Hibernate perform depending on the entity state. Our examples will be based on a simple Spring project. Manipulating entity states While using ORM libraries we trigger database changes by manipulating entities states. Entity class represents database table and entity instance represents row inside this table.  There are two different interfaces which can be used to handle entity state transitions: EntityManager Session Both enable us to manipulate Persistence Context . Persistence Context is responsible for handling entity states. The difference is that  EntityManager  is the JPA specification implementation while...

InputStreams & OutputStreams - good practices

Java.io package provides methods and classes used to handle input and output operations using data streams. We will have a look at good practices, concentrating on handling data streams using InputStream and OutputStream classes.  Usage of BufferedStreams When using unbuffered streams each write/read is handled by underlying operating system layer which then triggers disk access, network operation etc. There is a possibility to provide much better performance of Java software by minimazing calls to operating system. This is done with buffered streams. BufferedInputStream  reads data fron an in-memory area which is called buffer. Native call to OS layer is performed only when buffer is empty. This call loads big chunk of data to the buffer. After that,  BufferedInputStream    reads data from buffer (memory) rather than performing calls to OS layer,  which is much faster and has better performance.   Below is the example of how to create a...

HashMap - how it works?

Obraz
With maps we can access stored objects (values) using keys assigned to them. One of Map interface implementation is HashMap   class. In this article we will have a look how  HashMap  can be used and how it works under the hood.  Basic Usage Lets have a look at basic usage of HashMap : We have used HashMap  to be able to quickly map a person's name to his car. It is a typical use case of a map.  As you can see Car class overrides two methods from Object class -  hashCode()  and  equals() . Those will be used by  HashMap   internally, as described in next section. It is important to remember two things: hashCode()  is used to assign a number to an object.  There is a contract between   hashCode() and equals() which must be fulfilled - when equals() returnes true for compared objects, then hashCode() values must be the same for both objects. To learn more about  hashCode()  have a...

hashCode() - how to implement it? Where is it used?

Method  hashCode() is inherited from Object class . Its purpose is to return an    int  value, which can be used to represent given object . There is a contract between   hashCode()   and  equals()  which says that whenever  equals()   method used to compare two objects return s true, then values returned by  hashCode()  functions called on those objects must be the same . Remember that it does not work like that vice versa -  when  hashCode() values are the same , then it does not mean that  equals()  invoked for those two objects will always return true.  hashCode()  method is widely used by java containers like HashMap and HashSet . That is why it is desired that  hashCode()  will return unique values for unique objects as often as it is possible since it can improve performace of these containers (however it is not concidered as an error when  hashCode()  retur...

Non Blocking IO vs Blocking IO in simple client-server architecture

Obraz
Non Blocking IO (NIO) API is provided in java.nio package. It offers an alternative approach regarding handling input and output operations. One particular possibility is very interesting - performing non blocking IO operations. We will analyze this by impelmenting a simple client-server application and compare NIO approach with 'classical' blocking IO approach. Before we go through an example lets have a quick reminder of the most important NIO structures: Buffers - containers used to store data.  Channels - represent connections to resources with which we can perform IO operations. Channels use Buffers to read data from an external entity (file, socket etc.) into Buffer or write data into external resource from Buffer.  Selectors - enable taking advantage of non blocking IO operations. They are used together with SelectionKeys.  Lets cut to the chase and have a look at client-server example in classical blocking IO version. As an IO entity we will use socke...