Posts

Showing posts from 2020

What characters can a variable name consist of (a valid identifier) in Java

Image
The name or identifier of a variable is a sequence of lowercase and uppercase Latin letters, numbers, as well as the symbols "$" and "_". A variable name can start with any of the listed characters, except a number. It is technically possible to start a variable name with "$" or "_" as well, but this is prohibited by the Java Code Conventions. Also, the dollar symbol "$" is never used at all by convention. By convention, a variable name must begin with a lowercase letter (class names begin with a capital letter). Spaces are not allowed when naming variables. Read also: Methods for reading XML in Java XML, well-formed XML and valid XML XSD

Slices as arguments in Golang

Image
I recently came across several examples of using slices as function arguments, which lead to unexpected results. This post focuses on these examples. First example. The slice is passed to the function as an argument where it will be modified. https://play.golang.org/p/r5mKX5ErwLC package main import "fmt" func change(abc []int) { for i := range abc { abc[i] = 4 } fmt.Println(abc) } func main() { abc := []int{1, 2, 3} change(abc) fmt.Println(abc) } Output: [4 4 4] [4 4 4] Someone might expect that when we pass a slice to a function, we will get a copy of it in the function, and the changes will only appear in the function. But this is not the case. Slices in Go have a base array and are passed by reference, not value. We just pass the slice reference to the function, then change it, and it changes the outer slice. Second example. https://play.golang.org/p/5ruLrp6ZJJc package main import "fmt" func change(abc ...

volatile keyword in Java

Image
Defining a variable with the volatile keyword means that the value of that variable can be changed by other threads. To understand what volatile does, it is helpful to understand how threads handle regular variables. For performance reasons, the Java language specification allows the JRE to store a local copy of the variable for each thread that references it. These "local" copies of variables are like a cache and help a thread avoid accessing main memory every time it needs to retrieve the value of a variable. When two threads start, one of them reads variable A as 10, and the other as 20. If the value of variable A has changed from 10 to 20, then the first thread will not know about the change and will store the wrong value A. But if variable A is marked as volatile, then whenever the thread reads the value of A, it will access the master copy of A and read its current value. A thread-local cache makes sense if the variables in your applications will not be changed extern...

Methods for reading XML in Java

Image
DOM (Document Object Model) - object - reads XML, recreating it in memory in the form of an object structure, while the XML document is represented as a set of tags - nodes. Each node can have an unlimited number of child nodes. Each child can also contain several levels of descendants or none at all. Thus, the result is a kind of tree. Pluses: Easy to program. If there are many objects in XML with cross-references to each other, it is enough to go through the document twice: first time create objects without references and fill in the name-object dictionary, the second time - restore links. If an error occurs in XML, a half-created XML structure remains in memory, which will be automatically destroyed. Suitable for both reading and writing. Minuses: Low speed. Wastes a lot of memory. SAX (Simple API for XML) - event-driven - reads an XML document, responding to emerging events (opening or closing tag, string, attribute) by calling the event handlers provided by the appl...

XSD

Image
XSD, XML Schema Definition, XML Schema - a language for describing the structure of an XML document. In particular, XML Schema describes: dictionary - names of elements and attributes; content model - the relationship between elements and attributes, as well as their structure of the document; used data types. The advantages of XSD over DTD are as follows: DTD, unlike XSD, is not XML and has its own syntax. As a result, various problems with encoding and verifying XML documents can arise. When using XSD, the XML parser can check not only the syntax of an XML document, but also its structure, content model, and data types. In XML DTD, there is only one data type - a string, and if, for example, there is text in a numeric field, then the document will still be able to pass verification, since the XML DTD will not be able to check the data type. You cannot map more than one DTD to one XML document. Therefore, a document can be verified with only one DTD description. XSD is exte...

XML, well-formed XML and valid XML

Image
XML, the eXtensible Markup Language, is a language with a simple formal syntax, well suited for creating and manipulating documents with programs, and at the same time easy to read and create documents by humans. XML is extensible, it does not fix the markup used in documents, and the developer is free to create markup in accordance with the needs of a particular area, being limited only by the syntax rules of the language. DTD A DTD, Document Type Definition, is a predefined set of rules that defines the relationships between elements and attributes. For example, the DTD for HTML says that the DIV tag must be inside the BODY tag and can appear multiple times, TITLE - in HEAD and only once, and SCRIPT - both there and there as many times as you want. A DTD is usually described directly in the document as a formulation line starting with <!DOCTYPE ... > or in a separate file. Difference between well-formed XML and valid XML Depending on the level of compliance, the document...

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...