Two Laptop Bag*
The moose likes Spring and the fly likes Datasource url database name not known until after a Batch step Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Datasource url database name not known until after a Batch step" Watch "Datasource url database name not known until after a Batch step" New topic
Author

Datasource url database name not known until after a Batch step

Peter Brown
Greenhorn

Joined: Sep 26, 2008
Posts: 22
Hello,

Been doing my reading (swirling around right now) and can't fix my current issue.

I have 5 datasources, three of which I can fill in 100% through a property file. The other 2 I have everything but the db name at the end of the url.

(An aside: am I correct in reading that I therefore need 5 Transaction Managers? - If so will follow up with a new question thread)

The schema is the same for db1 so no changes to rowmappers etc - just the db name (same principle for db2).

I came across Dynamic DataSource Routing which I worked through and got working as I try and understand how Spring does things.

I got 'USING THE STEP EXECUTION CONTEXT AND PROMOTING THE DATA TO THE JOB EXECUTION CONTEXT' (Chapter 10 Spring Batch in Action) working by creating the db name in
TrackImportFromJobContextTasklet and retrieving it in VerifyStoreInStepContextTasklet.

I tweaked VerifyStoreInStepContextTasklet to see if I could set the udydb datasource url.



In launch-context I have


And the


gives



i.e. The datasource has not been instantiated yet (I think).

The next section in the book 'WRITING IN THE JOB EXECUTION CONTEXT AND READING USING LATE BINDING' won't work as in a different step.

So I am wrong in my thinking about the datasource and I think the code in VerifyStoreInStepContextTasklet breaches the whole separation of concerns?

I like the Dynamic Datasource Routing but can't figure out how to assign the db name which has been determined from an earlier step in the batch.

Any help appreciated. Thanks
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

Not sure if this is correct. But I would think then in the early step when you get the name, that you put that value into the JobExecutionContext, so that it can be available for later steps, readers/writers/processors. Would that help?

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

As far as TransactionManagers, if you are using a transactionManager that support XA/two-phase commits, like JTA, then you can have just one TransactionManager, as long as your DataSources are XA compliant. Otherwise, yes you would have to have a transactionManager per DataSource.

Mark
Peter Brown
Greenhorn

Joined: Sep 26, 2008
Posts: 22
Hi Mark - thanks for the help / pointers.

I tried



and on run it is giving me:



so will keep reading / experimenting...

Per the transactionManager - if I am not using the transactional atm concept (i.e. money must be debited at the same time as withdrawn across different databases) but inserts / updates are single database specific I am thinking I can use the 1 transactionManager?
[Within a step might be an insert to a db on another machine but since that step will be tied to a jdbc writer and so its dataSource, commits / rollbacks should be handled by the 1 transactionManager, right?]

So far my ideas are half-baked! ;-)

Peter
Peter Brown
Greenhorn

Joined: Sep 26, 2008
Posts: 22
Okay - I got the Datasource url database name not known until after a Batch step - fixed.

Here is what I have:

In my launch-context.xml (relevant parts)


The 'envTarget' is to allow the job to read different properties files e.g. development / production. I did have to put
a legitimate jdbc url string for the client.udy.url entry as otherwise I got sql errors. (As a precaution I have
created an empty dummy db so nothing returned on reads etc.)

My module-context.xml job section looks like:



VerifyStoreInStepContextTasklet (implements Tasklet) has:



TestBasicDataSource (implements Tasklet) has:



The next step in the job 'udytranStep' is my check to see what records are actually returned - it correctly connects to the batch job assigned db url and returns expected results.
Thanks to Mark for his help and hope this helps another newbie.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Datasource url database name not known until after a Batch step