Posty

Wyświetlanie postów z grudzień, 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