Posts

Showing posts from September, 2020

Execution of queries with JDBC

Image
What are the database queries made with? Java uses three interfaces to query the database: java.sql.Statement - for SQL statements without parameters; java.sql.PreparedStatement - for SQL statements with parameters and frequently executed statements; java.sql.CallableStatement - for executing procedures stored in the database. Interface bearers are created using the methods of the java.sql.Connection object: java.sql.createStatement() returns a Statement object; java.sql.prepareStatement() returns a PreparedStatement object; java.sql.prepareCall() returns a CallableStatement object; Difference between Statement and PreparedStatement Statement: Used for simple query cases without parameters. PreparedStatement: precompiles a query that can contain input parameters and be executed multiple times with a different set of those parameters. Before execution, the DBMS parses each query, optimizes it, and creates a query plan for its execution. If the same query is executed se...

Transaction isolation levels supported in JDBC

Image
Transaction isolation level is a value that determines the level at which inconsistent data is allowed in a transaction, that is, the degree of isolation of one transaction from another. A higher level of isolation improves the accuracy of the data, but it can reduce the number of concurrent transactions. On the other hand, a lower level of isolation allows more concurrent transactions, but reduces the accuracy of the data. During the use of transactions, to ensure data integrity, the DBMS uses locks to block other access to the data involved in the transaction. Such locks are necessary to prevent: "dirty" read - reading data, added or modified by a transaction, which later will not be confirmed (rolled back); non-repeatable read - when re-reading within one transaction, previously read data is changed; phantom reads - a situation when, when reading repeatedly within the same transaction, the same sample gives different sets of rows. Transaction isolation levels are de...

Basic steps for working with a database using JDBC

Image
Registration of drivers; Establishing a connection to the database; Creation of query(s) to the database; Executing the query(s) to the database; Processing the result(s); Closing the connection to the database. Register the JDBC driver Driver registration can be done in several ways: java.sql.DriverManager.registerDriver(%driver class object%) Class.forName("fully qualified driver class name").NewInstance() Class.forName("fully qualified driver class name") Establish a connection to the database The java.sql.DriverManager.getConnection(...) static call is used to establish a connection to the database. The parameter can be passed: Database URL static Connection getConnection(String url) Database URL and set of properties to initialize static Connection getConnection(String url, Properties info) Database URL, username and password static Connection getConnection(String url, String user, String password) As a result of the cal...

Parts of JDBC

Image
JDBC has two parts: JDBC API, which contains a set of classes and interfaces that define access to databases. These classes and methods are declared in two packages - java.sql and javax.sql; JDBC driver, a component specific to each database. JDBC turns API calls into native commands for a particular database server. List the main JDBC classes and interfaces java.sql.DriverManager - Lets you download and register the required JDBC driver, and then get a connection to the database. javax.sql.DataSource - solves the same tasks as DriverManager, but in a more convenient and versatile way. There are also javax.sql.ConnectionPoolDataSource and javax.sql.XADataSource whose job it is to provide connection pooling. java.sql.Connection - Provides data source queries and transaction management. The javax.sql.PooledConnection and javax.sql.XAConnection interfaces are also provided. java.sql.Statement, java.sql.PreparedStatement, java.sql.CallableStatement - These interfaces allow you t...

JDBC

Image
JDBC, Java DataBase Connectivity (connection to databases in Java) - the industry standard for the interaction of Java applications with various DBMS. Implemented as the java.sql package included with Java SE. JDBC is based on the concept of drivers that allow you to get a connection to a database at a specially described URL. When loaded, the driver registers itself in the system and is then automatically called when the program requires a URL containing the protocol for which this driver is responsible. Benefits of using JDBC The advantages of JDBC are: Ease of development: the developer may not know the specifics of the database with which he works; The code practically does not change if the company moves to another database (the number of changes depends solely on the differences between the SQL dialects); There is no need to additionally install the client program; Any database can be connected via an easily descriptive URL. JDBC URL JDBC URL consists of: <protoco...

Stored Procedure, Triggers, Cursor

Image
Stored Procedure Stored procedure is a database object that is a set of SQL statements stored on the server. Stored procedures are very similar to ordinary procedures in high-level languages, they can have input and output parameters and local variables, they can perform numeric calculations and operations on symbolic data, the results of which can be assigned to variables and parameters. Stored procedures can perform standard database operations (both DDL and DML). In addition, loops and branches are possible in stored procedures, that is, they can use instructions to control the execution process. Stored procedures improve performance, enhance programmability, and support data security features. In most DBMSs, the first time a stored procedure is run, it is compiled (parsed and a data access plan is generated) and further processing it faster. Trigger Trigger is a stored procedure of a special type, which the user does not call directly, but whose execution is conditioned by the ...

Data integrity constraints in SQL

Image
PRIMARY KEY is a set of fields (1 or more), the values of which form a unique combination and are used to uniquely identify a record in the table. Only one such constraint can be created for a table. This constraint is used to enforce the integrity of the entity that is described by the table. CHECK is used to restrict the set of values that can be placed in a given column. This constraint is used to ensure the integrity of the domain, which is described by the tables in the database. UNIQUE ensures that there are no duplicates in a column or set of columns. FOREIGN KEY protects against actions that could break relationships between tables. FOREIGN KEY in one table points to PRIMARY KEY in another. Therefore, this restriction is intended to ensure that there are no FOREIGN KEY entries that do not correspond to PRIMARY KEY entries. Differences between PRIMARY and UNIQUE constraints By default, the PRIMARY constraint creates a clustered index on a column, and UNIQUE creates a no...

IN, BETWEEN, LIKE operators in SQL

Image
IN - defines a set of values. SELECT * FROM Persons WHERE name IN ('Ivan','Petr','Pavel'); BETWEEN defines a range of values. Unlike IN, BETWEEN is order sensitive, and the first value in the sentence must be the first in alphabetical or numeric order. SELECT * FROM Persons WHERE age BETWEEN 20 AND 25; LIKE is applicable only to fields of type CHAR or VARCHAR with which it is used to find substrings. As a condition, wildcards are used - special characters that can match something: _ stands for any single character. For example, 'b_t' will match the words 'bat' or 'bit', but will not match 'brat'. % replaces a sequence of any number of characters. For example '%p%t' will match 'put', 'posit', or 'opt', but not 'spite'. SELECT * FROM UNIVERSITY WHERE NAME LIKE '%o'; UNION keyword In SQL, the UNION keyword is used to combine the results of two SQL queries int...

HAVING, ORDER BY, GROUP BY, COUNT in SQL

Image
HAVING is used to filter the result of a GROUP BY by a given boolean condition. Difference between HAVING and WHERE clauses HAVING is used as WHERE, but in a different part of the SQL statement and, accordingly, at a different stage in the formation of the answer. ORDER BY operator ORDER BY orders the query output according to the values in a given number of selected columns. Numerous columns are ordered one within the other. It is possible to define ASC ascending or DESC decreasing for each column. The default is ascending. GROUP BY clause GROUP BY is used to aggregate result records based on specified attributes. When using GROUP BY, all NULL values are considered equal. Difference between GROUP BY and DISTINCT clauses DISTINCT indicates that only unique column values are used for calculations. NULL counts as a separate value. GROUP BY creates a separate group for all possible values (including NULL). If only duplicates need to be removed, it is better to use DISTINCT...

JOIN in SQL

Image
JOIN is an SQL operator that implements the join operation of relational algebra. It is designed to provide data selection from two tables and include this data in one result set. The features of the join operation are as follows: the schema of the result table includes the columns of both source tables (operand tables), that is, the result schema is a "concatenation" of the operand schemas; each row of the result table is a "concatenation" of a row from one operand table with a row of the second operand table; if it is necessary to join not two, but several tables, the join operation is applied several times (sequentially). SELECT field_name [,... n] FROM Table1 {INNER | {LEFT | RIGHT | FULL} OUTER | CROSS } JOIN Table2 {ON <condition> | USING (field_name [,... n])} Types of JOINs (INNER) JOIN The result of joining tables are records that are common to the left and right tables. The order of the tables is not important for the operat...

SQL statements

Image
SQL, Structured query language, is a formal, non-procedural programming language used to create, modify, and manipulate data in an arbitrary relational database managed by an appropriate database management system (DBMS). SQL statements Data Definition Language (DDL) statements: CREATE creates a database object (database, table, view, user, etc.) ALTER modifies the object DROP removes the object Data Manipulation Language (DML) operators: SELECT selects data that meets the specified conditions INSERT adds new data UPDATE modifies existing data DELETE deletes data Data Control Language (DCL) statements: GRANT grants the user (group) permissions for certain operations with the object REVOKE revokes previously issued permissions DENY specifies a deny that takes precedence over permission Transaction Control Language (TCL) statements: COMMIT applies a transaction ROLLBACK rolls back all changes made in the context of the current transaction SAVEPOINT splits the tra...

Transactions in databases

Image
Transaction is an impact on a database that transfers it from one consistent state to another and is expressed in changing the data stored in the database. Main properties of the transaction Atomicity ensures that no transaction is partially committed in the system. Either all of its suboperations will be executed, or none of them will be executed. Consistency . A transaction that reaches its normal completion and thus commits its results maintains database consistency. Isolation . During the execution of a transaction, concurrent transactions should not affect its outcome. Durability . Regardless of problems at the lower levels (for example, a system outage or hardware failures), changes made by a successfully completed transaction should remain saved after the system is returned to work. Levels of transaction isolation In order to increase the isolation of transactions and, accordingly, the reliability of working with data: Read uncommitted (dirty read) - read uncommitted ...

Difference between clustered and non-clustered indexes

Image
Nonclustered indexes - data is physically arranged in random order, but logically ordered according to the index. This type of index is suitable for frequently changing datasets. With cluster indexing, the data is physically ordered, which significantly increases the speed of data retrieval (but only in the case of sequential data access). Only one clustered index can be created per dataset. Does it make sense to index data that has a small number of possible values? Approximate rule that can be followed when creating an index is that if the amount of information (in bytes) that does NOT satisfy the sampling condition is less than the size of the index (in bytes) for the given sampling condition, then, in general, optimization will slow down the sampling. When is full dataset scan superior to index access? Full scan is performed by multi-block reading. Index scan - single block. Also, when accessing by index, the index itself is scanned first, and then blocks are read from the dat...

Types of indexes in database

Image
By sort order ordered - the indices in which the elements are ordered increasing decreasing unordered - indices at which the items are unordered By data source view indexes expression indices By influence on the data source clustered index - When defined in a dataset, the physical location of the data is rebuilt according to the structure of the index. The logical structure of the dataset in this case is more of a dictionary than an index. The data in the dictionary is physically ordered, such as alphabetically. Clustered indexes can provide significant improvements in data search performance, even when compared to regular indexes. The performance increase is especially noticeable when working with sequential data. nonclustered index is the most common member of the index family. Unlike clustered, they do not rebuild the physical structure of the dataset, but only organize links to the corresponding records. To identify the required record in the data set, the noncluster...

Index in database

Image
Index - a database object created to improve the performance of data retrieval. Datasets can have a large number of records, which are stored in random order, and it can take a long time to search for them according to a given criterion by sequentially scanning the dataset, record by record. The index is formed from the values of one or several fields and pointers to the corresponding records of the data set, thus, a significant increase in the speed of retrieving from this data is achieved. Benefits acceleration of searching and sorting by a specific field or set of fields ensuring the uniqueness of data Disadvantages the requirement for additional space on disk and in RAM, and the larger/longer the key, the larger the index size Slower insert, update, and delete operations because the indexes themselves have to be updated. Indexes are preferred for : Counter fields to avoid duplicate values in this field; The fields by which the data is sorted; Fields that are frequ...

Normalization in databases

Image
Normalization is the process of transforming database relationships to a normal form (a step-by-step, reversible process of replacing the original schema with another schema in which the datasets have a simpler and more logical structure). Normalization is intended to bring the database structure to a form that provides minimal logical redundancy, and is not intended to decrease or increase the performance or decrease or increase the physical volume of the database. The ultimate goal of normalization is to reduce the potential inconsistency of information stored in the database. Normal forms First Normal Form (1NF) - relationship is in 1NF if the values ​​of all its attributes are atomic (indivisible). Second normal form (2NF) - relationship is in 2NF if it is in 1NF, and all non-key attributes depend only on the whole key, and not on any part of it. Third normal form (3NF) - relationship is in 3NF if it is in 2NF and all non-key attributes are independent of each other. Fourt...

Database and keys

Image
Database is a set of information organized and adapted for processing by a computer system. Database management system (DBMS) is a set of general or special purpose tools that provide creation, access to materials and database management. The main functions of the DBMS: data management data change logging data backup and recovery support for data definition and manipulation language Relational data model Relational data model is logical data model and applied theory of building relational databases. The relational data model includes the following components: Structural aspect - data is a set of relationships. Integrity aspect - relationships meet certain integrity conditions: domain (data type) level, relationship level, and database level. Aspect of processing (manipulation) - support for operators to manipulate relations (relational algebra, relational calculus). Normal form is a property of relation in relational data model that characterizes it in terms of redu...

Common tasks performed in servlet container in Java

Image
Data exchange support . The servlet container provides an easy way to exchange data between a web client (browser) and a servlet. Thanks to the container, there is no need to create a socket listener on the server to track requests from the client, as well as parse the request and generate a response. All these important and complex tasks are solved using the container and the developer can focus on the business logic of the application. Servlet and resource lifecycle management . From loading the servlet into memory, initializing, injecting methods, and ending with destroying the servlet. The container also provides additional utilities such as JNDI to manage the resource pool. Multithreading support . The container itself creates a new thread for each request and provides it with the request and response for processing. This way, the servlet is not re-initialized for every request, thereby saving memory and reducing the time before the request is processed. JSP support . JSP clas...

How does servlet container manage servlet lifecycle in Java

Image
The servlet container manages four phases of the servlet lifecycle: Servlet class loading - when the container receives a request for a servlet, the servlet class is loaded into memory and its constructor without parameters is called. Servlet class initialization - after the class is loaded, the container initializes the ServletConfig object for this servlet and injects it through the init() method. This is where the servlet class gets converted from a regular class to a servlet. Handling Requests - once initialized, the servlet is ready to process requests. For each client request, the servlet container spawns a new thread and calls the service() method by passing a reference to the response and request objects. Disposal - when the container is stopped or the application stops, then the servlet container destroys the servlet classes by calling the destroy() method. Thus, servlet is created the first time it is accessed and lives for the entire duration of the application (a...

Servlet container and application servers in Java

Image
Servlet container is a server program that provides system support for servlets and ensures their life cycle in accordance with the rules defined in the specifications. It can act as a full-fledged stand-alone web server, provide pages for another web server, or integrate into a Java EE application server. The servlet container provides data exchange between the servlet and clients, takes on such functions as creating a software environment for a functioning servlet, identifying and authorizing clients, organizing a session for each of them. The most famous implementations of servlet containers are: Apache Tomcat Jetty JBoss WildFly GlassFish IBM WebSphere Oracle Weblogic Why do you need application servers when there are servlet containers? Database connection pools The ability to periodically test the availability of the DBMS and update the connection in the event of failure recovery Replacing access rights when connecting Load balancing between several DBMS, determ...

Structure of servlet web project in Java

Image
This is example structure of servlet web project. src/main/java Application/library sources src/main/resources Application/library resource files src/main/filters Servlet filter files src/main/webapp Web application sources src/test/java Test sources src/test/resources Test resource files src/test/filters Servlet filter tests src/it Integration tests src/assembly Assembly Description src/site Site LICENSE.txt Project License NOTICE.txt Notes and Dependency Library Definitions. README.txt Project Description Read also: Servlet in Java List of principles for multithreaded programming in Java Thread safe Singleton in Java

Servlet in Java

Image
A servlet is an interface whose implementation extends the functionality of a server. A servlet communicates with clients through a request-response principle. While servlets can serve any request, they are commonly used to extend web servers. Most of the classes and interfaces needed to create servlets are contained in the javax.servlet and javax.servlet.http packages. Basic Servlet Methods: public void init(ServletConfig config) throws ServletException is thrown as soon as the servlet is loaded into memory public ServletConfig getServletConfig() returns a reference to an object that provides access to servlet configuration information public String getServletInfo() returns a string containing information about the servlet, for example: author and version of the servlet public void service(ServletRequest request, ServletResponse response) throws ServletException, java.io.IOException is thrown to process each request public void destroy() is executed before unloading the servle...

List of principles for multithreaded programming in Java

Image
When writing multithreaded programs, you should adhere to certain rules that help ensure decent application performance, combined with convenient debugging and ease of further code maintenance. Always give meaningful names to your streams. The process of debugging, finding errors, or tracing an exception in multi-threaded code can be challenging. OrderProcessor, QuoteProcessor, or TradeProcessor are much more informative than Thread1, Thread2 and Thread3. The name should reflect the task being performed by this thread. Avoid blocking or try to reduce sync scales. Blocking is expensive, and context switching is even more resource intensive. Try to avoid synchronization and blocking as much as possible, and organize the critical section to the minimum necessary. Therefore, the synchronized block is always preferable to the synchronized method, additionally giving the possibility of absolute control over the lock scale. Handle flow interruptions with extreme care. There is nothing wor...

Thread safe Singleton in Java

Image
1. Static field public class Singleton { public static final Singleton INSTANCE = new Singleton(); } 2. Enum public enum Singleton { INSTANCE; } 3. Synchronized Accessor public class Singleton { private static Singleton instance; public static synchronized Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } } 4. Double Checked Locking & volatile double checked locking Singleton is one way to create a thread-safe class that implements the Singleton pattern. This method tries to optimize performance by blocking only when the loner is instantiated for the first time. public class Singleton { private static volatile Singleton instance; public static Singleton getInstance() { Singleton localInstance = instance; if (localInstance == null) { synchronized (Singleton.class) { localInstance =...

Fork/Join framework in Java

Image
The JDK 7 Fork/Join framework is a set of classes and interfaces that take advantage of the multiprocessor architecture of modern computers. It is designed to perform tasks that can be recursively broken down into small sub-tasks that can be done in parallel. Fork stage: a large task is split into several smaller subtasks, which in turn are also split into smaller ones. And so on until the task becomes trivial and can be solved in a sequential way. Join stage: then (optionally) there is a "folding" process - solutions of subtasks are united in some way until the solution of the whole problem is obtained. The solution of all subtasks (including the division into subtasks itself) occurs in parallel. For some tasks, the Join step is not required. For example, for parallel QuickSort, the array is recursively divided into smaller and smaller ranges until it degenerates into the trivial case of 1 element. Although, in a sense, Join will be necessary here as well, since there ...

ReadWriteLock in Java

Image
ReadWriteLock is an interface that extends the base Lock interface. It is used to improve performance in a multithreaded process and operates on a couple of related locks (one for read operations, the other for writes). A read lock can be held simultaneously by multiple reading threads until a writer appears. The write lock is exclusive. There is a class ReentrantReadWriteLock that implements the ReadWriteLock interface, which supports up to 65535 write locks and up to the same number of read locks. ReadWriteLock rwLock = new ReentrantReadWriteLock(); Lock rLock = rwLock.readLock(); Lock wLock = rwLock.writeLock(); wLock.lock(); try { // exclusive write } finally { wLock.unlock(); } rLock.lock(); try { // shared reading } finally { rLock.unlock(); } What is a "blocking method"? Blocking method - a method that blocks until the task is completed, for example, the accept() method of the ServerSocket is blocked while waiting for a client connect...

Differences between synchronized and ReentrantLock in Java

Image
Java 5 introduces the Lock interface to provide more efficient and finer control over resource locking. ReentrantLock is a common Lock implementation that provides Lock with the same basic behavior and semantics as synchronized, but with advanced features such as lock polling, wait for a lock of a given duration, and interruptable wait for a lock. In addition, it offers much better performance in highly competitive environments. What is a reentrant lock? Simply that there is a collection count associated with the lock, and if the thread that holds the lock acquires it again, the data reflects the increase, and then the lock needs to be released twice to actually unlock. This is analogous to synchronized semantics; if a thread enters a synchronous block protected by a monitor that is already owned by the thread, the thread will be allowed to continue functioning, and the lock will not be released when the thread exits the second (or subsequent) synchronized block, it will only be relea...

ThreadLocal variable in Java

Image
ThreadLocal is a class that allows having one variable to have a different value for each of the threads. Each thread - i.e. an instance of the Thread class - there is a table of ThreadLocal variables associated with it. The table keys are references to objects of the ThreadLocal class, and the values are references to objects "captured" by ThreadLocal variables, i.e. ThreadLocal variables differ from regular variables in that each thread has its own, individually initialized variable instance. The value can be accessed through the get() or set() methods. For example, if we declare a ThreadLocal variable: ThreadLocal<Object> locals = new ThreadLocal<Object>();. And then, in the stream, let's make locals.set(myObject), then the table key will be a reference to the locals object, and the value will be a reference to the myObject object. At the same time, for another thread it is possible to "put" a different value inside the locals. Note that Thread...

Thread dump in Java

Image
Differences between stack and heap in terms of multithreading Stack is a piece of memory that is closely related to threads. Each thread has its own stack, which stores local variables, method parameters and a call stack. A variable stored on the stack of one thread is not visible to another. Heap is a shared piece of memory that is shared among all threads. Objects, whether local or of any other level, are created on the heap. To improve performance, a thread usually caches values ​​from the heap onto its stack, in which case the volatile keyword is used to indicate to the thread that a variable should be read from the heap. How do I share data between two threads? Data between threads can be shared using a shared object or parallel data structures such as BlockingQueue. To control the stack size of a thread -Xss JVM startup parameter is used. Thread dump HotSpot-based Java runtimes only generate an HPROF dump. The developer has several interactive dump generation methods and o...