Using JdbcTemplate with a "Non-Spring-Bean" JNDI DataSource - spring

Page 342 of spring-framework-reference.pdf (bundled with spring-framework-3.1.0.M2) states, "The JdbcTemplate can be used within a DAO implementation through direct instantiation with a DataSource reference." However, it goes on to say, "The DataSource should always be configured as a bean in the Spring IoC container."
Does anyone know why the DataSource shouldn't be provided to a JdbcTemplate from a plain-old JNDI lookup outside of the Spring container, e.g. How to programatically use Spring's JdbcTemplate?

"The DataSource should always be configured as a bean in the Spring IoC container."
It appears that this note is intended to clarify the preceding statement:
"The JdbcTemplate can be used within a DAO implementation through direct instantiation with a DataSource reference, or be configured in a Spring IoC container and given to DAOs as a bean reference."
I believe the information these statements are trying to convey is that when you're configuring a DAO in Spring, you can either:
inject the DataSource directly into the DAO and create the JdbcTemplate in code yourself, or
you can make the JdbcTemplate a Spring bean as well, inject the DataSource into the JdbcTemplate, and inject the JdbcTemplate into the DAO.
The note, then, means that if Spring is managing the DAO and its dependencies, the DataSource must be a Spring bean in either case, as it needs to be injected either into the DataSource for use in constructing the JdbcTemplate (case 1) or into the JdbcTemplate itself (case 2).
I wouldn't take it to mean that a DataSource used in a JdbcTemplate must always be managed by Spring and only Spring. The note does give that impression. It's probably worth filing a bug against.

Related

Binding specific sqlsessionfactory with #Mapper annotation with spring boot

TL;DR
When using MyBatis with Spring boot, is following step possible?
Create Datasource, SqlSessionFactory, SqlTransactionManager beans without mapperscan
Deploy 1 as library
import 2, create Mapper with beans declared in 1 with #Mapper annotation
Question
(First of all, apologize my poor english)
I'm creating mybatis datasource autoconfiguration library for my spring boot project.
Datasource will used at some applications, and they would have different mapper.
If mappers are already defined, creating DataSource, SqlSessionFactory and SqlTransactionManager beans with #MapperScan can bind Mapper and those beans.
But when case like this; DataSource, SqlSessionFactory, SqlTransactionManager beans declared,
and Mappers will created later, is possible binding specific beans with #Mapper annotation?
Those beans are default currently, so project would works.
But if another datasource added without mapper scan,
application initialization failed due to duplicate bean definition.
For some reasons, i prefer these done with mapper annotation(not mapper xml)
If any another advice for this design, looking forward to answer.
Thank you.

Why creation of repository beans need a datasource bean during start-up

I was working on a Spring-data-jpa project with spring boot, I see that the creation of repository beans required a datasoure bean to be present, Why is it so?
And can a repository bean be created without datasource bean.
The purpose of a repository is to load and save data into a persistent store.
Spring Data JPA does that using JPA so it needs an EntityManager which in turn need a DataSource.
Strictly speaking the DataSource is only used once the database is actually accessed.
While you definitely nee a DataSource bean you may delay the construction of a normal DataSource by providing a wrapper which instantiates the the actual DataSource at a later point in time.
DelegatingDataSource might be of help, either as a basis class or, since you are going to change the DataSource as a template for an implementation.
See the somewhat related question https://stackoverflow.com/a/61208585/66686

Does it make sense to use PersistenceExceptionTranslationPostProcessor with Spring's JdbcTemplate?

Title speaks for itself.
Is PersistenceExceptionTranslationPostProcessor used solely for JPA implementations or is it relevant to use it with Spring's JdbcTemplate too?
And if there are two datasources needed each with their own JPA entity manager and transaction manager, do I still only need to specify one PersistenceExceptionTranslationPostProcessor for the entire application?
The auto-award bounty answer is wrong
~~~The correct answer is as follows~~~
I believe I've discovered the answer here:
http://www.deroneriksson.com/tutorials/java/spring/introduction-to-the-spring-framework/component-scanning-and-repository
The #Repository annotation can have a special role when it comes to converting exceptions to Spring-based unchecked exceptions. Recall that the JdbcTemplate handled this task for us. When we work with Hibernate, we’re not going to work with a Spring template that handles this conversion of Hibernate-based exceptions to Spring-based exceptions. As a result, in order to handle this conversion automatically, Hibernate DAOs annotated with #Repository will have their Hibernate exceptions rethrown as Spring exceptions using a PersistenceExceptionTranslationPostProcessor.
Further reading: http://www.deroneriksson.com/tutorials/java/spring/introduction-to-the-spring-framework/hibernate-daos
The paragraph above explicitly says:
Recall that the JdbcTemplate handled this task for us
So, to answer my own question, there is no need to use PersistenceExceptionTranslationPostProcessor with jdbcTemplate
Yes you can, The Spring exception translation mechanism can be applied transparently to all beans annotated with #Repository – by defining an exception translation bean post processor bean in the Context:
<bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
As per it doc
Bean post-processor that automatically applies persistence exception translation to any bean marked with Spring's #Repository
annotation, adding a corresponding
PersistenceExceptionTranslationAdvisor to the exposed proxy (either an
existing AOP proxy or a newly generated proxy that implements all of
the target's interfaces).
Translates native resource exceptions to Spring's DataAccessException hierarchy. Autodetects beans that implement the
PersistenceExceptionTranslator interface, which are subsequently asked
to translate candidate exceptions.
All of Spring's applicable resource factories (e.g. LocalContainerEntityManagerFactoryBean) implement the
PersistenceExceptionTranslator interface out of the box. As a
consequence, all that is usually needed to enable automatic exception
translation is marking all affected beans (such as Repositories or
DAOs) with the #Repository annotation, along with defining this
post-processor as a bean in the application context.
So you can use it with Jdbctemplate as well as with any Jpa vendor implementation
As per is doc All of Spring's applicable resource factories (e.g. LocalContainerEntityManagerFactoryBean) implement the PersistenceExceptionTranslator interface out of the box, I think you still need to use PersistenceExceptionTranslationPostProcessor, because it used to translate all errors generated during the persistence process (HibernateExceptions, PersistenceExceptions...) into DataAccessException objects.

Transaction in Spring with datasources create in runtime

I have a problem with transaction in spring, because in my project datasources are created in runtime from side files and according to documentation:
I should inject to TransactionManager dataSource and made it visible for annotation #Transactional using <tx:annotation-driven transaction-manager="txManager"/>.
So my question is how can I do it when I want to use annotation?
First of all how are you creating the datasources at runtime.
if directly as Datasource=new datasource... I will suges use BeanDefinitionBuilder in Spring 3.2 to Create the Datasourcebean and then register it via BeanDefinitionRegistry.
and then get the bean from Spring context and it will be considered using the transaction.

How to get a DBUnit DatabaseConnection instance from Spring JdbcTemplate instance

I'm trying to use a Spring JdbcTemplate instance to generate a DataSet useable for subsequent DBUnit tests.
any ideas how to do that?
all the documentation I found where going from a JDBC Connection instance to a IDatabaseConnection instance.
But the code I have abstract all this away using Spring, and what I have is a JdbcTemplate instance.
Any ideas?
Your jdbcTemplate bean has a dataSource property, so you can either get it from the jdbcTemplate with its getter or inject the dataSource (which you already have defined somewhere in your applicationContext) in your class where you build the IDatabaseConnection and use it as a constructor-arg for that. (you should do the latter)
See the following blog for a detailed explanation and full example (written by a springsource trainer)
http://blog.zenika.com/index.php?post/2010/02/04/Testing-SQL-queries-with-Spring-and-DbUnit%2C-part-1
http://blog.zenika.com/index.php?post/2010/02/05/Testing-SQL-queries-with-Spring-and-DbUnit%2C-part-2

Resources