Posty

Wyświetlanie postów z 2019

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

Spring Data 1# - how repositories work under the hood

Obraz
Spring Data repositories enable programmers to manage entities in more advanced and convenient way than with plain EntityManager . It is interesting how repositories are working behind the scenes. There is very little to be done on the developer side comparing to what is happening under the hood. Quick reminder on Spring Data repositories Spring Data repositories can significantly reduce the amount of boilerplate code used for  data access purposes and manage entities in more complex way .  The main interface - Repository  is extended by CrudRepository which provides methods used to perform CRUD (Create Read Update Delete) operations on entities. Lets have a brief look at some of the core Spring Data and JPA repositories features: Already existing repositories enable us to perform basic CRUD and query operations. It is possible to define queries just by using proper repository method names. Annotations are widely used in repositories to configure data access layer - writing

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  Session  is the Hiber

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  BufferedInputStream  : There is

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 look at the artcile -  hashCode() - how it works? How it works under the

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()  returns the same value for different objects). How to implement it? Whenever you override an  equals() method, you will

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 sockets whi