Prev Next

Spring / Spring Data Access

1. Explain JdbcTemplate in Spring framework. 2. Advantages of Spring JDBC Template over standard JDBC. 3. How could I access Hibernate using Spring? 4. How can we integrate Spring and Hibernate using HibernateDaoSupport? 5. What are the advantages of transaction management in Spring Framework? 6. Which Transaction management type is preferred in Spring? 7. Explain DataAccessException in Spring. 8. How do I configure DAO in Spring? 9. What are the ORMs does Spring supports? 10. List some of the no sql databases does spring support? 11. Difference between Spring Data-JPA versus JPA. 12. Give few examples of spring callbacks used in JdbcTemplate. 13. What are the exceptions thrown by the Spring DAO classes? 14. What is Spring Data JPA? 15. What is SQLExceptionTranslator in Spring DAO? 16. Difference between CrudRepository and JpaRepository interfaces in Spring Data JPA. 17. Difference between Spring Data JPA and hibernate. 18. How do I decide between JPA and Spring JdbcTemplate? 19. What is PreparedStatementCreator in Spring Data framework? 20. What is SQLProvider in Spring Data framework? 21. What is BatchPreparedStatementSetter in Spring Data? 22. Which is the better way of using JDBC in Spring? 23. What template does Spring JDBC provide to access database? 24. What are the native NativeJdbcExtractor in Spring? 25. Explain Global and Local transactions in Spring. 26. How do we roll back a declarative transaction in Spring? 27. Different types of isolation in spring transaction management. 28. Types of propagation in Spring transaction management. 29. Spring: How do I rollback a declarative transaction? 30. Spring: List out the role of TransactionDefinition interface. 31. What are the benefits of the Spring transaction management ? 32. Why Spring Declarative transaction management is preferred over programmatic? 33. Spring @Transactional Annotation. 34. What is the principle of Declarative transaction management in Spring? 35. How does @Transactional works in Spring? 36. How do you prevent SQL Injection in Java Code? 37. Advantages of using @RepositoryRestResource annotation in Spring Data REST. 38. When to use @RestController vs @RepositoryRestResource annotations in Spring. 39. How do I unit test spring data repositories? 40. What are the different isolation levels of DBMS? 41. What is the difference between serializable and repeatable read isolation level? 42. Explain serializable isolation level. 43. What is the ISOLATION_DEFAULT in spring transaction management. 44. What is the default propagation behavior of spring transaction management? 45. Does the transaction rollback on exception in spring declarative transaction management? 46. Explain NoRepositoryBean spring annotation. 47. How do you handle High-volume Transaction Processing in J2EE? 48. Explain Dirty read. 49. Difference between non-repeatable read and phantom read. 50. What is 2 phase commit? 51. Difference between JDBC and connection pooling. 52. When to use JTA and JPA transaction manager? 53. Can I use multiple transaction managers within a spring application? 54. Difference between Spring JtaTransactionManager and HibernateTransactionManager. 55. Difference between HibernateTransactionManager and JpaTransactionManager. 56. Difference between DataSourceTransactionManager and HibernateTransactionManager. 57. Does Spring Transactional annotation support both global and local transactions? 58. is Spring transactional annotation be applied only for public methods? 59. Why applying Spring Transactional annotation on the concrete class or its methods is recommended rater than on interface? 60. Explain proxy-target-class attribute of spring transactions. 61. What are the default @Transactional settings? 62. What are the different Spring propagation levels? 63. What is Spring Data? 64. Describe the purpose of the @Transactional(propagation = Propagation.NESTED) annotation.

1. Explain JdbcTemplate in Spring framework.

JdbcTemplate class provides many convenience methods for doing things such as converting database data into primitives or objects, executing prepared and callable statements, and providing custom database error handling.

Read full answer

2. Advantages of Spring JDBC Template over standard JDBC.

The Spring JDBC Template has the following advantages compared with standard JDBC. The Spring JDBC template allows to clean-up the resources automatically, e.g. release the database connections. The Spring JDBC template converts the standard JDBC SQLExceptions into RuntimeExceptions. This allows ...

Read full answer

3. How could I access Hibernate using Spring?

There are 2 ways to access Hibernate with Spring. Inversion of Control with a Hibernate Template and Callback. Extending HibernateDAOSupport and Applying an AOP Interceptor node.

Read full answer

4. How can we integrate Spring and Hibernate using HibernateDaoSupport?

Use Spring's SessionFactory called LocalSessionFactory to integrate Spring and HIbernate. The integration process is of 3 steps. Configure the Hibernate SessionFactory. Extend a DAO Implementation from HibernateDaoSupport. Wire in Transaction Support with AOP.

Read full answer

5. What are the advantages of transaction management in Spring Framework?

It supports declarative transaction management. It provides a consistent programming model across different transaction APIs such as JTA, JDBC, Hibernate, JPA, and JDO. It provides a simpler API for programmatic transaction management than a number of complex transaction APIs such as JTA. It inte...

Read full answer

6. Which Transaction management type is preferred in Spring?

Declarative transaction management is preferred over programmatic transaction management though it is less flexible than programmatic transaction management, which allows you to control transactions through your code.

Read full answer

7. Explain DataAccessException in Spring.

This exception hierarchy aims to let user code find and handle the kind of error encountered without knowing the details of the particular data access API in use (for example, JDBC). This class is a runtime exception, there is no need for user code to catch it or subclasses if any error is to be ...

Read full answer

8. How do I configure DAO in Spring?

Mark the DAO class with @Repository annotation. Entity manager can be configured by @PersistenceContext at property level.

Read full answer

9. What are the ORMs does Spring supports?

Spring supports the following ORMS. Hibernate. iBatis. JPA (Java Persistence API). TopLink. JDO (Java Data Objects).

Read full answer

10. List some of the no sql databases does spring support?

MongoDB , Open-source document database. Cassandra Database that provides scalability and high availability without compromising performance. Redis structure server wherein keys can contain strings, hashes, lists, sets, and sorted sets. HBase Hadoop database, a distributed and scalable big data s...

Read full answer

11. Difference between Spring Data-JPA versus JPA.

JPA is a specification while Hibernate is a particular implementation of that specification. Spring Data JPA is on top of JPA adding another layer of abstraction, standardizing the design for the support of your Persistence Layer in a Spring context. Those defined interfaces (known to Spring) pro...

Read full answer

12. Give few examples of spring callbacks used in JdbcTemplate.

The Spring callbacks in JdbcTemplate are PreparedStatementCallback, ResultSetExtractor, RowCallbackHandler and RowMapper.

Read full answer

13. What are the exceptions thrown by the Spring DAO classes?

Spring DAO classes throw exceptions that are subclasses of org.springframework.dao. DataAccessException . Spring translates technology-specific exceptions like SQLException to its own exception class hierarchy with the DataAccessException as the root exception that are generic and easy to underst...

Read full answer

14. What is Spring Data JPA?

Spring Data JPA aims to significantly improve the implementation of data access layers by reducing the effort to the amount that is actually needed. As a developer you write your repository interfaces, including custom finder methods, and Spring will provide the implementation automatically.

Read full answer

15. What is SQLExceptionTranslator in Spring DAO?

SQLExceptionTranslator is a strategy interface that translates between SQLExceptions and Spring's data access strategy-agnostic DataAccessException hierarchy. The returned DataAccessException is supposed to contain the original SQLException as root cause.

Read full answer

16. Difference between CrudRepository and JpaRepository interfaces in Spring Data JPA.

JpaRepository extends PagingAndSortingRepository that extends CrudRepository. CrudRepository mainly provides CRUD operations. PagingAndSortingRepository provide methods to perform pagination and sorting of records. JpaRepository provides JPA related methods such as flushing the persistence contex...

Read full answer

17. Difference between Spring Data JPA and hibernate.

spring-data adds powerful and useful API that helps with complex queries. Spring JPA will provide you abstraction from writing SQL and even some HQL using query method declaration. Spring JPA shines with its query generation but when you want a purely hibernate solution you can customize as neede...

Read full answer

18. How do I decide between JPA and Spring JdbcTemplate?

Use Spring JdbcTemplate if you don't want to access your database schema via a domain model. Using JPA you need to make sure that database schema maps correctly to the domain model. Performance is almost similar at both spring JdbcTemplate and JPA.

Read full answer

19. What is PreparedStatementCreator in Spring Data framework?

PreparedStatementCreator is one of the most commonly used interfaces for writing data to the database. createPreparedStatement() is a method that can be used to create and return PreparedStatement from the Connection argument, and exception handling is automatically taken care of. When this inter...

Read full answer

20. What is SQLProvider in Spring Data framework?

SQLProvider is mainly used for debugging. It has only one method called getSql() and it is implemented using PreparedStatementCreator implementers.

Read full answer

21. What is BatchPreparedStatementSetter in Spring Data?

BatchPreparedStatementSetter is used to update more than a single row in bulk.This interface provides two methods, setValues( PreparedStatement ps, int i) throws SOL exception. int getBatchSize.

Read full answer

22. Which is the better way of using JDBC in Spring?

For better performance and easy implementation JDBC can be used with the template class called JdbcTemplate.

Read full answer

23. What template does Spring JDBC provide to access database?

JdbcTemplate. NamedParameterJdbcTemplate. SimpleJdbcTemplate

Read full answer

24. What are the native NativeJdbcExtractor in Spring?

SimpleNativeJdbcExtractor. C3P0NativeJdbcExtractor . CommonsDbcpNativeJdbcExtractor . JBossNativeJdbcExtractor.

Read full answer

25. Explain Global and Local transactions in Spring.

Global transactions helps manage multiple transactional resources like relational databases and message queue. Global transactions are managed through JTA and JNDI. Local transactions are resource-specific like JDBC connection. Local Transactions can interact with multiple transactional resources.

Read full answer

26. How do we roll back a declarative transaction in Spring?

Use rollback-for and no-rollback-for attributes with transactional definition.

Read full answer

27. Different types of isolation in spring transaction management.

ISOLATION_DEFAULT : default isolation. ISOLATION_READ_COMMITTED : dirty reads are prevented, non-repeatable and phantom reads are allowed. ISOLATION_READ_UNCOMMITTED : dirty reads are allowed, no-repeatable and phantom reads are allowed. ISOLATION_REPEATABLE_READ : dirty reads and non-repeatable ...

Read full answer

28. Types of propagation in Spring transaction management.

There are 7 propagation levels in spring. PROPAGATION_MANDATORY : supports current transaction and throws exception if no transaction available. PROPAGATION_NESTED : runs with nested transaction. PROPAGATION_NEVER : does not run with current transaction and throws exception if current transaction...

Read full answer

29. Spring: How do I rollback a declarative transaction?

Use rollback-for and no-rollback-for attributes with transactional definition.

Read full answer

30. Spring: List out the role of TransactionDefinition interface.

Isolation. Propagation. Timeout. Read-only status.

Read full answer

31. What are the benefits of the Spring transaction management ?

The Spring Framework provides a consistent abstraction for transaction management that delivers the following benefits: Provides a consistent programming model across different transaction APIs such as JTA, JDBC, Hibernate, JPA, and JDO. Supports declarative transaction management. Provides a sim...

Read full answer

32. Why Spring Declarative transaction management is preferred over programmatic?

Declarative transaction management provides least impact on application code and hence is most consistent with the ideals of a non-invasive lightweight container.

Read full answer

33. Spring @Transactional Annotation.

Transactional annotation is the convenient and readable and is currently the recommended way to handle transactions in Spring. By using @Transactional, many important aspects such as transaction propagation are handled automatically.

Read full answer

34. What is the principle of Declarative transaction management in Spring?

Declarative transaction support uses AOP (Aspect Oriented Programming) as its foundation.

Read full answer

35. How does @Transactional works in Spring?

@Transactional annotation is based on AOP concept. When you annotate a method with @Transactional, Spring dynamically creates a proxy that implements the same interface(s) as the class you are annotating. And when clients make calls into your object, the calls are intercepted and the behaviors ge...

Read full answer

36. How do you prevent SQL Injection in Java Code?

You can use PreparedStatement to avoid SQL injection in Java code. Use of the PreparedStatement for executing SQL queries not only provides better performance but also shield your Java and J2EE application from SQL Injection attack.

Read full answer

37. Advantages of using @RepositoryRestResource annotation in Spring Data REST.

@RepositoryRestResource annotation exposes a data repository as a RESTFul resource. This annotiation is similar to @RestController annotation in Spring MVC.

Read full answer

38. When to use @RestController vs @RepositoryRestResource annotations in Spring.

@RestController annotation renders as Restful resource within a Controller while @RepositoryRestResource annotation exposes repository itself as a RESTful resource.

Read full answer

39. How do I unit test spring data repositories?

Unit testing can be achieved by mocking all JPA data API that we invoke. However the unit testing for spring data respositories is not required since we don't write any API implementations. Integration testing is more ideal than unit testing.

Read full answer

40. What are the different isolation levels of DBMS?

Serializable, Repeatable reads, Read committed, and read uncommitted.

Read full answer

41. What is the difference between serializable and repeatable read isolation level?

In a Repeatable Read isolation level, new rows can be inserted into the dataset. In a Serializable isolation level all the rows are locked for the duration of the transaction, no insert,update or delete is allowed.

Read full answer

42. Explain serializable isolation level.

This is the highest isolation level. With a lock-based concurrency control DBMS implementation, serializability requires read and write locks (acquired on selected data) to be released at the end of the transaction. Also range-locks must be acquired when a SELECT query uses a ranged WHERE clause,...

Read full answer

43. What is the ISOLATION_DEFAULT in spring transaction management.

Spring ISOLATION_DEFAULT uses the default isolation level of the underlying datastore.

Read full answer

44. What is the default propagation behavior of spring transaction management?

PROPAGATION_REQUIRED is the default propagation property of @Transactional annotation.

Read full answer

45. Does the transaction rollback on exception in spring declarative transaction management?

By default configuration only unchecked exceptions (that is, subclasses of java.lang.RuntimeException) are rollbacked and the transaction will still be commited in case of checked exceptions. To enable rollbacking on checked exceptions add the parameter rollBackFor to the @Transactional attribute...

Read full answer

46. Explain NoRepositoryBean spring annotation.

This annotation excludes repository interfaces from being picked up and prevents getting an instance being created. The common usage of this annotation is when providing an extended base interface for all repositories in combination with a custom repository base class to implement methods declare...

Read full answer

47. How do you handle High-volume Transaction Processing in J2EE?

Leverage batch processing to process the work off-line in batch. Use Real-time asynchronous processing such as fine-grained timers or asynchronous event triggers (such as JMS) to pass responsibility from on-line to back-end function.

Read full answer

48. Explain Dirty read.

When one transaction reads changes made by another transaction that has not yet been committed it is called dirty read. It is dangerous and unusual to use this type of transaction because the read data might get rolled back.

Read full answer

49. Difference between non-repeatable read and phantom read.

Both non-repeatable and phantom reads have to do with data modification operations from a different transaction, which were committed after your transaction began, and then read by your transaction. Non-repeatable reads are when your transaction reads committed UPDATES from another transaction. T...

Read full answer

50. What is 2 phase commit?

Two phase commit is used in distributed environment where multiple transaction take part in distributed transaction process. In two phase commit, commit or rollback is performed in 2 phases: Commit request phase : In this phase main process or coordinator process send precommit request to all oth...

Read full answer

51. Difference between JDBC and connection pooling.

JNDI creates a connection pool. Connection pool is an environment on the server where JNDI and Database encapsulated to for Type4 connectivity. JDBC : A Java API that enables Java programs to execute SQL statements. This allows Java programs to interact with any SQL-compliant database.

Read full answer

52. When to use JTA and JPA transaction manager?

If you want to delegate managed transactions to your application server and handle complex transactions across multiple resources you need to use the JtaTransactionManager , but I believe it is not needed in most cases, J JpaTransactionManager , is the best choice. JTA is global transaction while...

Read full answer

53. Can I use multiple transaction managers within a spring application?

Yes. @Transactional annotation has the property transactionManager which can be set to specify which transaction manager to be used. ...

Read full answer

54. Difference between Spring JtaTransactionManager and HibernateTransactionManager.

HibernateTransactionManager manages transactions on top of a single Hibernate SessionFactory. If your application uses only a JDBC-compliant database to store data (that is, no ERP, JMS queue, file system, etc. is involved) that you access using Hibernate, you can use a HibernateTransactionManage...

Read full answer

55. Difference between HibernateTransactionManager and JpaTransactionManager.

It is about making your code depend on either JPA or Hibernate when using Hibernate or JPA transaction manager. javax.persistence.EntityManager is a JPA (Java EE) standard interface, while org.hibernate.SessionFactory is a Hibernate proprietary interface. Using EntityManager instead of SessionFac...

Read full answer

56. Difference between DataSourceTransactionManager and HibernateTransactionManager.

DataSourceTransactionManager is for JDBC while HibernateTransactionManager is for Hibernate. DataSourceTransactionManager needs a reference to the DataSource, the HibernateTransactionManager needs a reference to the SessionFactory.

Read full answer

57. Does Spring Transactional annotation support both global and local transactions?

Yes. The Spring Framework's declarative transaction management works in any environment. It can work with JTA transactions or local transactions using JDBC, JPA, Hibernate or JDO.

Read full answer

58. is Spring transactional annotation be applied only for public methods?

Yes, only public methods. If you annotate protected, private or package-visible methods with the @Transactional annotation, no error is raised, but the annotated method does not exhibit the configured transactional settings. Consider the use of AspectJ if you need to annotate non-public methods.

Read full answer

59. Why applying Spring Transactional annotation on the concrete class or its methods is recommended rater than on interface?

Spring recommends annotating concrete classes and its methods with the @Transactional annotation, as opposed to annotating interfaces. You can place the @Transactional annotation on an interface (or an interface method), but this works only if you are using interface-based proxies. Java annotatio...

Read full answer

60. Explain proxy-target-class attribute of spring transactions.

The proxy-target-class attribute controls what type of transactional proxies are created for classes annotated with the @Transactional annotation. If proxy-target-class is set to true, class-based proxies are created. If proxy-target-class is false or if the attribute is omitted, standard JDK int...

Read full answer

61. What are the default @Transactional settings?

Default propagation setting is PROPAGATION_REQUIRED. Isolation level is ISOLATION_DEFAULT. The transaction is read/write. Transaction timeout defaults to the default timeout of the underlying transaction system, none if timeouts are not supported. Any RuntimeException triggers rollback, and any c...

Read full answer

62. What are the different Spring propagation levels?

Propagation is the ability to decide how the business methods should be encapsulated in both logical or physical transactions. There are 7 different propagation levels. REQUIRED states that the same transaction will be used if there is an already opened transaction in the current bean method exec...

Read full answer

63. What is Spring Data?

Spring Data's mission is to provide a familiar and consistent, Spring-based programming model for data access while still retaining the special traits of the underlying data store. It makes it easy to use data access technologies, relational and non-relational databases, map-reduce frameworks, an...

Read full answer

64. Describe the purpose of the @Transactional(propagation = Propagation.NESTED) annotation.

The @Transactional(propagation = Propagation.NESTED) annotation is used to define a nested transactional scope in a Spring Boot application. When a method is annotated with this annotation, a nested transaction is created within the current transaction. The nested transaction behaves as an indepe...

Read full answer

«
»

Comments & Discussions