Ibatis" Need to manage connections/datasources independently - jdbc

The design of our application forces us to obtain our jdbc conncetions independently of the Ibatis session. We need a way to hand the connection (or alternately a datasource) to Ibatis and say "use this".
Have tries every search imaginable and can't find help.
Thanks in advance

If I understand you correctly, then you may configure your own JNDI that will be used by your application to get DataSource/Connection objects. Also you may pass this configured JNDI into iBatis:
<transactionManager type="EXTERNAL">
<dataSource type="JNDI">
<property name="DataSource"
value="java:comp/env/jdbc/someDataSource"/>
</dataSource>
</transactionManager>

Related

Spring application should start even database is not available at startup

I have an old spring application which uses jee:jndi-lookup for datasource. This application running on Tomcat 8.
<jee:jndi-lookup id="datasource" jndi-name="java:/comp/env/jdbc/Tomcat8Database" destroy-method="close" expected-type="javax.sql.DataSource" lookup-on-startup="false"/>
The database may be sometime down at startup of the application, but as I also tried to lazy-init spring beans it did not helped as what it seems like that JNDI lookup in spring happened on Startup always or its not in spring controls as Server provide Pooling over connections.
Any idea or code example will be helpful.
According to spring javadoc, For a lazy lookup, a proxy interface needs to be specified.
Proxy interface specify the proxy interface to use for the JNDI object.
Typically used in conjunction with "lookupOnStartup"=false and/or "cache"=false. Needs to be specified because the actual JNDI object type is not known in advance in case of a lazy lookup.
Try:
<jee:jndi-lookup id="datasource" jndi-name="java:/comp/env/jdbc/Tomcat8Database" destroy-method="close" expected-type="javax.sql.DataSource" lookup-on-startup="false" proxy-interface="javax.sql.DataSource"/>

Spring JdbcTemplate , How to dynamically set username and password in Spring config XML file?

Spring JDBC: while using JdbcTemplate how to set username and password for every user ?, Currently I am Configuring datasource object as a spring bean (Spring config.xml file ) and able to login with sinlge username and password , also used properties file and placeholder for the same
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/apu"></property>
<property name="username" value="root"></property>
<property name="password" value=""></property>
</bean>
What are the ways to set username value and password value for every user who logsin ?
This is my first Spring application hence not able to find right approach ..
Generally, in a web application, each user do not use its own identity to access the database. The common pattern is to have one single database user for the application, and let the application manage the permission of its own users.
As M.Deinum said, this pattern allows the application to use a connection pool, where database connections are recycled across different requests which dramatically reduces the database load (establishment of the connection is expensive)
If you look at (almost) all the spring examples an tutorial, they consitently use that approach with one single database user for the whole web application.
If you really need that each user uses it own database identity you must use a UserCredentialsDataSourceAdapter as suggested by M. Deinum, with no database pool.

Spring JNDI Configuration

We are using Spring to do JNDI look-up for our data access requirements. We are making calls to multiple stored procedures (in Sybase). I wanted a way to control the auto-commit property of the transactions while calling these stored procs. I was able to achieve it with BasicDataSource (property name="defaultAutoCommit").
But I got stuck when we moved to doing JNDI look-up (from Weblogic).
<jee:jndi-lookup jndi-name="WL_data_source" id="dataSource" />
<bean id="procedureHandlerBean" class="myclass">
<property name="procDataSource" ref="dataSource"/>
</bean>
After researching for a few days, it appears to me that I can use JtaTransactionManager. However, I am not sure if I can use the transaction manager to configure auto-commit on a per procedure call basis (due to different chained/unchained mode requirements of different procs). Is there a clean way to achieve this? Thanks

How to securely store database credentials in a Spring MVC web application?

I have a Spring MVC web application which needs to connect to a database, and the data source credentials are currently (in development) being kept in the application context configuration file, i.e. in WEB-INF\spring\application_context.xml like so:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="username" value="my_username" />
<property name="password" value="my_password" />
<property name="url" value="my_datasource_url" />
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
</bean>
Before I deploy this application to a public facing Tomcat server I want to make sure that I'm not making these database credentials visible to the world or easily discovered by a crafty hacker. Am I OK keeping things as they are, i.e. in plain text and in an XML file under WEB-INF? If not then is there a best practice for this sort of thing?
Thanks in advance for your help.
Files stored in WEB-INF folder are by definition inaccessible from the outside world. E.g. if you put JSP file there no user can access that file directly - it can be referenced for example by importing or including from another JSP.
That being said your credentials are safe, but it is not a good practice to hard code them in the applicationContext.xml. Are you pushing these credentials to your source control?
The safest way is to extract sensitive and frequently changing configuration in an external .properties file somewhere on your hard disk. Spring is perfectly capable of reading .properties files.
Another thing to consider would be to have a JNDI lookup. JNDI would be contained within the servlet container (tomcat in your case) allowing connections to the database only through webapps currently deployed from Tomcat.
This way also creates a silo'd experience between build management and development so development doesn't have the keys to the car, if you get my metaphor.
You could make it ask the password on startup. But usually what i see is passwords in xml files which are chmodded and chowned to be only accessible by the web software itself. There are no easy good solutions.
One basic step is to make sure that the tomcat-user has only the access it needs on the database , so if it is compromised, the damage is limited.

Where does business processing datasource config go in Spring Batch?

I have been learning the Spring Batch framework to try to put in practice at work through the online documentation as well as the Pro Spring Batch book by Appress. I have a quick question.
Scenario
I want to do a simple test where I read from the database, do some processing, and then write to another database.
Question
I understand that there is a configuration file called launch-context.xml that contains the Job Repository database schema to maintain the state of the jobs and each of the steps for each one of them.
Say that I have a Source Database (A) where I do a read from and a Target Database (B) where I write to.
Maybe I have overlooked it but...
Where do I put the data source information for A and B?
I guess it depends on the answer of #1 but if put it under src/main/resources say for example source-datasource.xml and target-datasource.xml How is Spring going to pick it up and wire it appropriately? In Spring web app development I usually put those types of files under the context-param tag.
You can define these datasources in any spring file of your choosing, so yes:
src/main/resources/db/source-datasource.xml
src/main/resources/db/target-datasource.xml
will do.
Let's say you named your datasource beans as a sourceDataSource and a targetDataSource. The way you tell Spring Batch ( or in this case just Spring ) to use them is through the "import" and "dependency injection".
Importing
You can organize your spring configs the way you fit best, but since you already have launch-context.xml, in order for the above datasources to be visible, you need to import them into launch-context.xml as:
<import resource="classpath:db/source-datasource.xml"/>
<import resource="classpath:db/target-datasource.xml"/>
Injecting / Using
<bean id="sourceReader" class="org.springframework.batch.item.database.JdbcCursorItemReader">
<property name="dataSource" ref="sourceDataSource" />
<!-- other properties here -->
</beans:bean>
<bean id="targetWriter" class="org.springframework.batch.item.database.JdbcBatchItemWriter">
<property name="dataSource" ref="targetDataSource" />
<!-- other properties here -->
</beans:bean>
where a sourceReader and a targetWriter are the beans you would inject into your step(s).

Resources