Skip to content

Commit b954f99

Browse files
committed
Transaction question and jdbc question has been answered
1 parent 8359436 commit b954f99

File tree

52 files changed

+365
-83
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+365
-83
lines changed

pom.xml

+5-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@
7575
<artifactId>h2</artifactId>
7676
<version>1.4.197</version>
7777
</dependency>
78-
78+
<dependency>
79+
<groupId>org.springframework</groupId>
80+
<artifactId>spring-orm</artifactId>
81+
<version>5.1.5.RELEASE</version>
82+
</dependency>
7983
</dependencies>
8084

8185
</project>

src/main/java/data/question_001/SpringException.java renamed to src/main/java/data_transaction_jpa/question_001/SpringException.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
package data.question_001;
1+
package data_transaction_jpa.question_001;
22

33

44
/**
55
* What is the difference between checked and unchecked exceptions?
66
* • Why does Spring prefer unchecked exceptions?
7-
* • What is the data access exception hierarchy?
7+
* • What is the data_transaction_jpa access exception hierarchy?
88
*
99
*
1010
*
1111
*
1212
* Checked exception should be managed in a try catch block or should be declared on the method signature.
1313
* unchecked exception don't have this restriction.
1414
* For this reason Spring Framework prefer to use the unchecked exception.
15-
* All the exceptions in the data access hierarchy are unchecked.
15+
* All the exceptions in the data_transaction_jpa access hierarchy are unchecked.
1616
* The purpose is to isolate the applications from particularity of vendor specification.
1717
*
1818
*/

src/main/java/data/question_02/MyDataSource.java renamed to src/main/java/data_transaction_jpa/question_02/MyDataSource.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
package data.question_02;
1+
package data_transaction_jpa.question_02;
22

33

44
import ioc.commun.Person;
5-
import org.springframework.beans.factory.annotation.Autowired;
65
import org.springframework.beans.factory.annotation.Value;
76
import org.springframework.context.ApplicationContext;
87
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@@ -20,7 +19,6 @@
2019
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
2120

2221
import javax.sql.DataSource;
23-
import java.sql.Date;
2422
import java.sql.ResultSet;
2523
import java.sql.SQLException;
2624
import java.time.LocalDate;
@@ -145,8 +143,8 @@ public DataSource secondDataSource (){
145143
public DataSource thirdDataSource (){
146144
final EmbeddedDatabase embeddedDatabase = new EmbeddedDatabaseBuilder()
147145
.setType(EmbeddedDatabaseType.H2)
148-
.addScripts("databaseconfig/ddl.script",//ddl data definition language define database schema
149-
"databaseconfig/dml.script")//dml data manipulation language CRUD operations
146+
.addScripts("databaseconfig/ddl.script",//ddl data_transaction_jpa definition language define database schema
147+
"databaseconfig/dml.script")//dml data_transaction_jpa manipulation language CRUD operations
150148
.build();
151149
return embeddedDatabase;
152150

src/main/java/data/question_03_04_05_06_07/MyJdbcTemplate.java renamed to src/main/java/data_transaction_jpa/question_03_04_05_06_07/MyJdbcTemplate.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package data.question_03_04_05_06_07;
1+
package data_transaction_jpa.question_03_04_05_06_07;
22

33
import org.springframework.jdbc.core.JdbcTemplate;
44

@@ -18,9 +18,9 @@
1818
* 1-Template design pattern define some steps which are executed in order, the super type define this order,
1919
* the subclasses implement only some predefined steps.
2020
* 2-
21-
* a- RowMapper<Object>: allows to handle rows in a result set row by row {@link data.question_02.MyDataSource#rowMapper(JdbcTemplate)}.
21+
* a- RowMapper<Object>: allows to handle rows in a result set row by row {@link data_transaction_jpa.question_02.MyDataSource#rowMapper(JdbcTemplate)}.
2222
* b-resultSetExtractor<List<Object>>:
23-
* allows to process the entire result set {@link data.question_02.MyDataSource#resultSetExtractor(JdbcTemplate)}.
23+
* allows to process the entire result set {@link data_transaction_jpa.question_02.MyDataSource#resultSetExtractor(JdbcTemplate)}.
2424
* c-@todo example to make for the RowCallbackHandler
2525
* * 3-yes
2626
* 4-Jdbc template release the connection after every request. jdbctemplate is trade safe
@@ -52,9 +52,9 @@ public final void templateMethod(){
5252
destroy();
5353
}
5454
public final void init() {}
55-
public void method1() {throw new RuntimeException("the method1 should be implemented");}
56-
public void method2() {throw new RuntimeException("the method2 should be implemented");}
57-
public void method3() {throw new RuntimeException("the method3 should be implemented");}
55+
public void method1() {throw new RuntimeException("the method1 should be implemented by the subclasses");}
56+
public void method2() {throw new RuntimeException("the method2 should be implemented by the subclasses");}
57+
public void method3() {throw new RuntimeException("the method3 should be implemented by the subclasses");}
5858
public final void destroy() {}
5959

6060

src/main/java/data/question_03_04_05_06_07/SqlQuery.java renamed to src/main/java/data_transaction_jpa/question_03_04_05_06_07/SqlQuery.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
package data.question_03_04_05_06_07;
1+
package data_transaction_jpa.question_03_04_05_06_07;
22

33

4-
import data.question_02.MyDataSource;
4+
import data_transaction_jpa.question_02.MyDataSource;
55
import ioc.commun.Person;
66
import org.springframework.context.ApplicationContext;
77
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package data_transaction_jpa.question_08_09_10_11;
2+
import org.springframework.context.annotation.Bean;
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
5+
import org.springframework.jdbc.datasource.DriverManagerDataSource;
6+
import org.springframework.stereotype.Service;
7+
import org.springframework.transaction.PlatformTransactionManager;
8+
import org.springframework.transaction.annotation.EnableTransactionManagement;
9+
import org.springframework.transaction.annotation.Isolation;
10+
import org.springframework.transaction.annotation.Propagation;
11+
import org.springframework.transaction.annotation.Transactional;
12+
13+
import javax.sql.DataSource;
14+
15+
/**
16+
*
17+
* How are you going to define transaction in Spring?
18+
* What does @Transactional do?
19+
* What is the PlatformTransactionManager?
20+
*
21+
* A transaction Manager {@link PlatformTransactionManager}, should exists on the classpath to enable transaction management.
22+
* enable transaction by annotating one configuration class {@link EnableTransactionManagement}.
23+
* @Transactional on classes or method that should run in transaction.
24+
*
25+
*
26+
* Is the JDBC template able to participate in an existing
27+
* transaction?
28+
* This is accomplished by wrapping the DataSource using a TransactionAwareDataSourceProxy.
29+
*/
30+
public class EnablingTransaction {
31+
32+
}
33+
34+
@Service
35+
@Transactional
36+
class Myservice{
37+
public void methodOne(int a){ }
38+
39+
//overriding the class annotation
40+
@Transactional(isolation = Isolation.SERIALIZABLE, propagation = Propagation.MANDATORY)
41+
public void methodTwo(int a){ }
42+
}
43+
44+
45+
46+
@EnableTransactionManagement
47+
@Configuration
48+
class MyConfiguration{
49+
50+
/**
51+
* defining a transaction manager
52+
* @return
53+
*/
54+
@Bean
55+
public PlatformTransactionManager transactionManager (){
56+
return new DataSourceTransactionManager(datasource());
57+
}
58+
59+
/**
60+
* defining a datasource
61+
* @return
62+
*/
63+
public DataSource datasource(){
64+
DriverManagerDataSource dataSource = new DriverManagerDataSource();
65+
dataSource.setUsername("");
66+
return dataSource;
67+
}
68+
69+
70+
71+
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package data_transaction_jpa.question_08_09_10_11;
2+
3+
4+
/**
5+
*
6+
* -What is a transaction?
7+
* A transaction in a unit of work that should either fully executed or fully canceled.
8+
* transaction principals are: ACID.
9+
* A: Atomicity, the same as the definition of the transaction.
10+
* C: Consistency, guarantee that evey database constraint will not be violated.
11+
* I: Isolation, isolation level that defines how more than one transactions are handled win parallel.
12+
* D: Durability, guarantee that when the transaction commit, the change are also stored into the database.
13+
* -What is the difference between a local and a global transaction?
14+
* Local transaction: when we deel with inly one single transactional resource.
15+
* Global transaction: more than one transactional resource, handled by the app-server.
16+
*
17+
* Is a transaction a cross cutting concern? YEs
18+
* How is it implemented by Spring? implemented using AOP Spring framework
19+
*
20+
*/
21+
public class MyTransaction {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package data_transaction_jpa.question_12_13;
2+
3+
4+
/**
5+
* What is @EnableTransactionManagement for?
6+
* Enable to use @Transactional, One configuration class should be annotated.
7+
*
8+
*
9+
*/
10+
public class EnablingTransactionManagement {
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package data_transaction_jpa.question_12_13;
2+
import org.springframework.transaction.annotation.Isolation;
3+
4+
/**
5+
* Problems we can get when two or more transactions are executing in parallel on the same data_transaction_jpa.
6+
* {@link IsolationLevel.READ_UNCOMMITTED} is the slowest isolation level, T1 can Read uncommitted data_transaction_jpa from T2.
7+
* This Isolation level can produces Dirty Read problem:
8+
* On one cell for example, T1 read 10, T2 write 15,T1 read 15, T2 rollback, T1 get bad result.
9+
* T1---10---------25---
10+
* T2---10-----15----10-
11+
*
12+
* Dirty read occurs when T1 read data_transaction_jpa that was not yet committed by another Transaction.
13+
* To solve this problem we should use {@link IsolationLevel.READ_COMMITTED}
14+
*
15+
* With {@link IsolationLevel.READ_COMMITTED}, non-repeatable read problem can be produced.
16+
* T1---10---------25---
17+
* T2--------15---------15
18+
* On one cell for example, T1 read 10, T2 write 15, T1 in a second time read the same cell, as it was updated
19+
* by T2, T1 add 10, the value now is 25(bad result).
20+
*
21+
*
22+
* With {@link IsolationLevel.REPEATABLE_READ}, phantom read(read-insertion problem) problem can be produced.
23+
* T1---1---------2---
24+
* T2--------2--------
25+
* Suppose we have a table person(name, age)
26+
* For example, T1 with a query operation(get person.name where person.age>10), suppose also that in T1 we get one row as a response.
27+
* suppose now that T2 insert a new row[person('zaki', 30)] and commit. T1 in the second read operation will get 2 rows as a response.
28+
* The problem here is maybe In the operation In T1, we are performing an algorithm and we should have the same value of te read operation.
29+
* To solve this problem we should use {@link Isolation.Isolation.SERIALIZABLE}. This isolation guarantee that for example, every person with person.age>10
30+
* can not be read or written by another transaction.
31+
*
32+
* {@link https://www.byteslounge.com/tutorials/spring-transaction-isolation-tutorial}
33+
*
34+
*/
35+
public class IsolationLevel {
36+
}
Loading
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package data_transaction_jpa.question_14_15_16_17_18_19;
2+
3+
/**
4+
*
5+
*
6+
* What is the default rollback policy in a JUnit test, when you use the
7+
* @RunWith(SpringJUnit4ClassRunner.class) in JUnit 4 or
8+
* @ExtendWith(SpringExtension.class) in JUnit 5, and annotate your @Test annotated
9+
* method with @Transactional?
10+
*
11+
* Rollback after every test executed in a transactional mode, but we can change this default behaviour,
12+
*
13+
*
14+
*/
15+
public class TestingWithTransactionalMode {
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package data_transaction_jpa.question_14_15_16_17_18_19;
2+
import org.springframework.context.annotation.Bean;
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.transaction.annotation.Propagation;
5+
import org.springframework.transaction.annotation.Transactional;
6+
7+
/**
8+
*
9+
* 1-What does transaction propagation mean?
10+
* Behaviour of a transaction in relation to another transaction, there are 7 propagation types:
11+
* T1
12+
* a(){
13+
* T2
14+
* b();
15+
* }
16+
* {@link Propagation#REQUIRED} //default in Spring
17+
* T2 will use T1 if T1 exists, will create a new Transaction otherwise.
18+
* {@link Propagation#REQUIRES_NEW}
19+
* every time, T2 will crete a new transaction.
20+
* {@link Propagation#NESTED}
21+
* T2 will use T1 if T1 exists, will create a new Transaction otherwise. If T2 rollback, T1 will rollback to.
22+
* {@link Propagation#SUPPORTS}
23+
* use T1 if it exists, do nothing otherwise
24+
* {@link Propagation#NOT_SUPPORTED}
25+
* suspend T1 if it exists, do nothing otherwise
26+
* {@link Propagation#NEVER}
27+
* throw an exception if T1 exist, do nothing otherwise
28+
* {@link Propagation#MANDATORY}
29+
* throw an exception if T1 doesn't exist, use T1 otherwise
30+
*
31+
* 2-What happens if one @Transactional annotated method is calling another @Transactional annotated method on the same object instance?
32+
* the inner method will not run on a transactional mode.
33+
* 3-Where can the @Transactional annotation be used? What is a typical usage if you put in class level?
34+
* 4-What does declarative transaction management mean?
35+
* Class, method level. method override class level. All method in a annotated class level will be automatically annotated.
36+
* 5-What is the default rollback policy? How can you override it?
37+
* rollback whenever an unchecked exception (RuntimeException or Error)is thrown from a method.
38+
* thrown in this scenario means that it was not cached by a try block catch in the method.
39+
* 6- rollback policy overridden by :
40+
* rollBackFor
41+
* rollBackForClassName
42+
* noRollBackFor
43+
* noRollBackForClassName
44+
*
45+
*
46+
*/
47+
public class TransactionPropagation {
48+
49+
50+
51+
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package data_transaction_jpa.question_20_21_22_23_24;
2+
3+
4+
/**
5+
* Why is the term "unit of work" so important and why does JDBC AutoCommit violate this pattern?
6+
* unit of work--> atomicity.
7+
* Jdbc AutoCommit violate this pattern, because after each query,
8+
* a commit will be executed, so if we have many query we will not have the atomicity property.
9+
*
10+
*/
11+
public class JDBCAutoCommitMode {
12+
}

0 commit comments

Comments
 (0)