Wednesday, June 30, 2021

Make a Basic Spring Boot Project

 Make a Basic Spring Boot Project

  

Hello All, Today we will create a new Spring boot project from the scratch. It will be a bare minimum possible project. We will add the minimum dependency in the project.

 

We will use Intellij as an IDE.

 

First we will go to below link:

 https://start.spring.io/

 

You can fill the Group, Artifact, Name, Description of the project and packaging.

 

It is shown in the below image as well.

 

After entering the above details, you will click on 'Generate' button. It will save the project on your local system.

 



Now, import the project in Intellij IDE.

 

After importing it will be like below:




Congratulations!! you have made your first very basic and simplest possible spring boot project.

 

 

Now, we will run this project.

 

Go to follwoing path in the project:

src -> main -> java -> com -> example -> learningdemo -> LearningdemoApplication.java

 

Now, click on the run icon and click on the first option to run the main method of LearningdemoApplication class.

 

 

It is shown in follwing snap:


 

 

Now, you will see the output of the project like below;



Congratulations Again !! you have run the very basic and simplest spring boot project.






 

 

 

 

Tuesday, June 29, 2021

Transaction Isolation Levels in RDBMS

 Transaction Isolation Levels in RDBMS

 
A transaction is a single unit of operation we either execute it entirely or do not execute it at all.
 
ACID properties must be followed for a transaction operation to maintain the integrity of the database.
 
 
 
A: Atomicity
C: Consistency
I: Isolation
D: Durability
 
 
 
 
Atomicity: Either the transaction will be execute entirely or will not be executed at all.
 
 
Consistency: When the transaction has been executed then the database will move from one consistent state to another consistent state.
 
 
Isolation: Transaction should be executed in isolation of other transactions. 
So, during the current  transaction execution, intermediate transaction results of another (concurrently running) transaction should not be available to each other.
 
Two concurrent transactions should not impact the another transaction's flow/data. Even if these 2 transactions are running concurrently, but the result should be like as they would have run sequentially.
Although, we will talk about this in detail later in this article.
 
 
Durability: After successful completion of the transaction, the changes in the database should persist. Even if the application server/system gets restarted or failed.
 
 
 
 
 
- Different isolation levels describe - how changes applied by concurrent transactions are visible to each other.
- Each isolation level prevents zero or more concurrency side effects on a transaction. Ex: dirty read, nonrepeatable-read, phantom read.



1. Dirty Read: Read the uncommitted changes of a concurrent transaction.
2. Non Repeatable Read: Get different value on re-read(in a single transaction only) of a row if a concurrent transaction updates the same row and commits.
3. Phantom Read: Get different rows after re-execution of a range query if another transaction adds or removes some rows in the range and commits.





Different Transaction Isolation Levels:

There are 5 types of isolation levels:
 
 
1. READ_UNCOMMITTED: 
- This is the lowest level isolation(not supported in Postgres, Oracle)
- We can set isolation level for method or class.
- This level suffers from all the 3 above mentioned concurrency side effects.
- This level allows for most concurrent access.
 
 
 
2. READ_COMMITTED: (Default in Postgres, SQL Server, Oracle)
- This isolation level prevents dirty read. So, any uncommitted changes in concurrent transactions have no impact on us, but if a transaction commits it's changes then we can get different results when we do re-query.



3. REPEATABLE_READ: (Default in Mysql, Oracle doesn't support)
- This isolation level prevents dirty and non-repeatable reads. So,we are not affected by uncommitted changes in concurrent transactions.
- When we re-query for a row, we don't get different result but if we re-execute the range query , we might get newly added or removed rows.
- This is the minimum required isolation level to prevent the lost update. (Lost Update happens when 2 or more concurrent transactions read and update the same row.)
- This level does not allow simultaneous access to row at all. Hence lost update can't happen.



4. SERIALIZABLE:
- This is the highest level of isolation.
- This isolation level prevents all the side effects.
- In this isolation level, lowest concurrent access rate, since this level executes concurrent calls sequentially.


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Transaction Propagation Types in Spring

Transaction Propagation Types in Spring



There are 7 types of propagation types:

1. Propagation.REQUIRED

@Transactional(propagation=Propagation.REQUIRED)

- This is default propagation type.
- If there is already an active transaction, then this propagation does not do anything and uses that transaction scope only. In another case, where there is not any active transaction, then it creates new transaction.



2. Propagation.SUPPORTS

@Transactional(propagation=Propagation.SUPPORTS)
 
- If there is already an active transaction, then this propagation does not do anything and uses that transaction scope only. Otherwise, this method runs non-transactional.



3. Propagation.MANDATORY

 @Transactional(propagation=Propagation.MANDATORY)
 
- If there is already an active transaction, then this propagation does not do anything and uses that transaction scope only. Otherwise throws an IllegalTransactionStateException.



4. Propagation.NEVER

@Transactional(propagation=Propagation.NEVER)
 
 - If there is already an active transaction, then this propagation throws an IllegalTransactionStateException.
 
 
 

5. Propagation.NOT_SUPPORTED

@Transactional(propagation=Propagation.NOT_SUPPORTED)
 
 - If there is already an active transaction, then this propagation suspends that transaction and runs non-transactional.


 

6. Propagation.REQUIRES_NEW

@Transactional(propagation=Propagation.REQUIRES_NEW)
 
 - If there is already an active transaction, then this propagation suspends that transaction and creates a new transaction.
- In another case of not having any active transaction, this creates new transaction.


 

7. Propagation.NESTED

@Transactional(propagation=Propagation.NESTED)
 
 - If there is already an active transaction, then spring marks a savepoint, so if the method throws any exception then transaction gets rollback to this savepoint.
- In another case of not having any active transaction, works as Propagation.REQUIRED.
























Saturday, February 1, 2020

What is Deposit Insurance

Bank Deposit Insurance:




If a person deposits his/her money in a bank then maximum 5 lakhs money is insured according to the new budget proposal. So, if the person wants to withdraw his/her money from the bank, then he will guaranteed get minimum of (deposited money OR insured 5 lakhs amount). 

Previously this limit was of  1 lakh. 

This scheme insures all types of bank deposits including savings, fixed and recurring with an insured bank. 

The bank deposits are insured by Deposit Insurance and Credit Guarantee Corporation (DICGC), a subsidiary of the Reserve Bank of India. The agency does not directly charge any premium from bank depositors but banks pay a nominal premium for the cover.

This 5 lakhs insurance amount includes both principle and interest amount.

This insured amount is summed up for all the branches of a bank, but not for different banks. So, if we keep money in multiple banks then from each bank we will get maximum deposit insurance of 5 lakhs.


Ex:
Bank1(branch1) : deposit amount - 2 lakhs
Bank1(branch2) : deposit amount - 4 lakhs
Bank2(branch1) : deposit amount - 7 lakhs
Bank3(branch1) : deposit amount - 3 lakhs

if all the banks bank1, bank2 and bank3 are getting closed at the same time(although it is very unlikely to happen), then from bank1 we will get guaranteed 5 lakhs. same as from bank2 and from bank3 we will get 3 lakhs.

so, total we can loose 1 lakh(from bank1), 2 lakhs(from bank2) and 0 (from bank3).




If you have any doubt or need more information about the topic, please write down in the comment section, will get clarified soon.




Wednesday, January 15, 2020

Multi Threading Short Notes


Thread vs Process:

- Process actually does not run but threads run. So, internally (in the process) there will be at least one thread which will be doing the actual task/work.

- Threads (of the same process) run in a shared memory space, while processes run in separate memory spaces.

- Thread is termed as 'light weight process', since it is similar to a real process but executes within the context of a process and shares the same resources (address space/process memory, data) allotted to the process by the kernel.


Main Thread:
- JVM starts the main thread.

- This is the first thread that is created after Java application gets started.

- It is the thread from which other 'child' threads will be spawned.

- It must be the last thread to finish execution bcz it performs various shutdown actions.




Thread class: (in java.lang package)

- states: available as enums in Thread class.


              Image: represents the state diagram
   

- A thread is in the blocked state, if it tries to access a protected section of code that is currently locked by some other thread. Whenever thr protected section is unblocked, the scheduler picks one thread (from blocked states) and moves that thread to runnable state from the blocked state.

- A thread is in the waiting state when it waits for another thread on a condition, when this condition is fulfilled, the scheduler is notified and waiting thread is moved to runnable state.

 t.getState() : method to get the current state.




Synchronized Method vs Block:

Synchronized Block :
- reduces the scope of the lock
- we can use arbitrary any lock(object) for mutual exclusion.
- can throw NullPointerException if the lock expression is null.

Synchronized Method:
- Entire method will be synchronized.
- lock will be on 'this' object or on the class (if the method is static)
- will not be null, so will not throw NullPointerException.




Inter-thread Communication:

  object lock == monitor

- Communication happens via 3 ways: (wait(), notify() or notifyAll())
- These methods are called from the synchronized context (otherwise IllegalMonitorStateException will be thrown if a thread has been instructed to wait for an object's monitor that the specified thread does not have ownership of.)

wait() method: (defined in Object class)

- Can be called on the object, on which the current thread has lock.
- Necessary to have lock on that object.
- Thread will release the lock on the object and will go in wait state.
- Should call wait() method inside while loop instead of if block bcz it's possible that thread wakes up spuriously even the waiting condition is not changed.




Race Condition:
majorly occurs in 2 situations:
1. Check and act
2. Read modify write

(The problem here is our assumption that each line of code is atomic, BUT it is NOT).
ex: '++' operator is not an atomic operation.


  • Check and Act:


Ex1:
if(!hashTable.contains(key)){
    hashTable.put(key, value);
}
Here, both the operations (contains() and put()) are atomic individually but are not atomic together.
So, here race condition can occur, if 2 threads checks the same result of contains() method.

Ex2:
public Singleton getInstance(){
    if(_instance == null){
         _instance = new Singleton();
    }
}
Here also, if 2 threads read _instance's value as null -> both the threads will enter inside if block. Same problem will occur.




  • Read Update Write:

i++ is not an atomic operation.
so, if not synchronized then race condition will occur here.
OR
we can use AtomicInteger, which is internally thread-safe. Then no need to synchronized externally.





Thread Safety:
- Immutable objects are thread-safe.
- Readonly or final variables are thread-safe.
- Local variables are thread-safe bcz each thread has there own copy.
- Vector, concurrentHashMap, HashTable, String classes are thread-safe classes.





Dead Lock:
when 2 or more threads are waiting for each other to release the resource they need (lock) and get stuck for infinite time.
ex:
public void method1(){
   synchronized(String.class){
      synchronized(Integer.class){
         System.out.println("acquired both the locks and reached here");
      }
   }
}

public void method2{
  synchronized(Integer.class){
     synchronized(String.class){
         System.out.println("acquired both the locks and reached here");
     }
  }
}     
Here, a good chance is present of deadlock.
Solution: should acquire locks in same order.



Important Methods inside Thread class:
1. join() method:
- final method
- nonstatic method
Ex: if we have 3 threads: T1, T2, T3
need to make sure that initially T1 will complete it's task then T2 will complete and then T3 will complete.
    In this requirement, we can use join() method.

t1.join();   // from T2
t2.join();   // from T3

Ex:
Thread exThread = new Thread() {
    public void run() {
        try{
             System.out.println(Thread.currentThread().getName() + "started");
             Thread.sleep(2000);
             System.out.println(Thread.currentThread().getName() + "ended");
         }catch(InterruptedException e){
             System.out.println("Exception caught");
         }
    }
};
exThread.start();
exThread.join();
             System.out.println(Thread.currentThread().getName() + "ended");


O/P: 
Thread-0 started
Thread-0 ended
main ended


2. sleep() method:
- static method
- pauses the current thread for specified miliseconds.
- does not release the lock.

Other ways to pause the current thread:

TimeUnit.SECONDS.sleep(4);
TimeUnit.MINUTES.sleep(4);
TimeUnit.HOURS.sleep(2);
TimeUnit.DAYS.sleep(1);

-> above are available in java.util.concurrent package (since Java 5).

3. yield() method:
- static method
- current thread goes from running state to runnable state and gives chance to another thread to go into running state. 
- So, after calling yield() method scheduler checks if there is any thread with same or higher priority than this thread. If found -> then it will move current thread to read/runnable state and give processor to other thread.















Please comment down any query/doubt regarding the topic, will try to clarify that ASAP.



Tuesday, January 14, 2020

Comparisons between Indian Domestic flights

Comparisons among Indian Domestic flights:

Common things for each flight:
1. We have to reach approx 2 hours before the departing time of flight/
2. We have to first check in either web check-in or counter check-in.
3. We have to present 25 minutes prior on the boarding gates in any way if we don't want to miss the flight.
4. You should have one photo id card (issued by some govt. institution or gov. org.), Passport, voter id, driving licence or Aadhar card will work, I assume.
5. Baggage: There are two types of baggages:
a) Checked Baggage b) Cabin baggage or hand baggage.
6. Baggage limit will be 15kg(checked-in baggage) + 7kg(cabin baggage). 
Dimensions: 


1. Vistara: It is the best domestic flight. All the reviews on different web-sites are good.
Pros:
1. Gives free food.
2. All the people/cabin crew in Vistara will be very helpful and very polite.
Cons:
1. Cost may be higher than other flights.


(2) Go Air: According to reviews on different sites, it is not very much good flight to be prefered unless you are out of other option. I found only negative points and reviews on web for Go Air.

Some informative facts about Go Air:
1. This does not provide free food.
2. Also the cost is average.
3. Cabin crew is not very much helpful and polite.
4. Not good in punctuality.

(3) Indigo: This is the mostly preferred flight for domestic within India.

Pros and Cons:
1. Cost is average and affordable.
2. Gives free food (not so sure about this)

(4) Air India: This is the another best flight to be preferred.

Pros and Cons:
1. Gives free food.
2. Seating space is good.




Privacy Policy Disclaimer

Privacy Policy for ankmit.blogspot.com

At ankmit.blogspot.com, accessible from ankmit.blogspot.com, one of our main priorities is the privacy of our visitors. This Privacy Policy document contains types of information that is collected and recorded by ankmit.blogspot.com and how we use it.

If you have additional questions or require more information about our Privacy Policy, do not hesitate to contact us.

Log Files

ankmit.blogspot.com follows a standard procedure of using log files. These files log visitors when they visit websites. All hosting companies do this and a part of hosting services' analytics. The information collected by log files include internet protocol (IP) addresses, browser type, Internet Service Provider (ISP), date and time stamp, referring/exit pages, and possibly the number of clicks. These are not linked to any information that is personally identifiable. The purpose of the information is for analyzing trends, administering the site, tracking users' movement on the website, and gathering demographic information.

Google DoubleClick DART Cookie

Google is one of a third-party vendor on our site. It also uses cookies, known as DART cookies, to serve ads to our site visitors based upon their visit to www.website.com and other sites on the internet. However, visitors may choose to decline the use of DART cookies by visiting the Google ad and content network Privacy Policy at the following URL – https://policies.google.com/technologies/ads

Privacy Policies

You may consult this list to find the Privacy Policy for each of the advertising partners of ankmit.blogspot.com. Our Privacy Policy was created with the help of the Privacy Policy Generator and the Privacy Policy Generator Online.

Third-party ad servers or ad networks uses technologies like cookies, JavaScript, or Web Beacons that are used in their respective advertisements and links that appear on ankmit.blogspot.com, which are sent directly to users' browser. They automatically receive your IP address when this occurs. These technologies are used to measure the effectiveness of their advertising campaigns and/or to personalize the advertising content that you see on websites that you visit.

Note that ankmit.blogspot.com has no access to or control over these cookies that are used by third-party advertisers.

Third Party Privacy Policies

ankmit.blogspot.com's Privacy Policy does not apply to other advertisers or websites. Thus, we are advising you to consult the respective Privacy Policies of these third-party ad servers for more detailed information. It may include their practices and instructions about how to opt-out of certain options. You may find a complete list of these Privacy Policies and their links here: Privacy Policy Links.

You can choose to disable cookies through your individual browser options. To know more detailed information about cookie management with specific web browsers, it can be found at the browsers' respective websites. What Are Cookies?

Children's Information

Another part of our priority is adding protection for children while using the internet. We encourage parents and guardians to observe, participate in, and/or monitor and guide their online activity.

ankmit.blogspot.com does not knowingly collect any Personal Identifiable Information from children under the age of 13. If you think that your child provided this kind of information on our website, we strongly encourage you to contact us immediately and we will do our best efforts to promptly remove such information from our records.

Online Privacy Policy Only

This Privacy Policy applies only to our online activities and is valid for visitors to our website with regards to the information that they shared and/or collect in ankmit.blogspot.com. This policy is not applicable to any information collected offline or via channels other than this website.

Consent

By using our website, you hereby consent to our Privacy Policy and agree to its Terms and Conditions.