Results 1 to 8 of 8

Thread: How to not specify a Data Source property for the transacion manager bean?

  1. #1
    Join Date
    Sep 2007
    Posts
    4

    Default How to not specify a Data Source property for the transacion manager bean?

    I'm pretty much completely new to Spring. I've been trying to get declarative transaction management to work.

    It does work, but only if I specify a data source in the transaction manager bean, as in the example:

    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSou rceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
    </bean>

    We get our data source from a separate config file. So I want to leave out the data source property, we don't want it in two config files:

    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSou rceTransactionManager">
    </bean>

    The DataSource is available to my code. I subclassed DataSourceTransactionManager. I can get the DataSource from our configuration at run time. For testing, I dummied it up as follows:

    public class MyDataSourceTransactionManager extends DataSourceTransactionManager {

    public MyDataSourceTransactionManager() {
    super();

    BasicDataSource basicDataSource = new BasicDataSource();
    basicDataSource.setDriverClassName("org.hsqldb.jdb cDriver");
    basicDataSource.setUrl("jdbc:hsqldb:db_file");
    basicDataSource.setUsername("sa");
    basicDataSource.setPassword("");
    basicDataSource.setDefaultAutoCommit(false);
    super.setDataSource(basicDataSource);
    }
    }

    This doesn't work, in the sense that my transactions are no longer atomic, each little piece commits. I tried to fix this by adding

    basicDataSource.setDefaultAutoCommit(false);

    as above. No luck.

    I hope this isn't too obvious a question, I'm new to Spring and have no idea how to proceed, any help would be much appreciated.

    Thanks,
    Michael

  2. #2
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    I'm not sure I completely understand what you are trying to do here. You have two applicationContext files and you delcare the transactionManager in one and the dataSource in the other? If that's the case that should be fine, you can load multiple applicationContext files and they can share beans between them. Is there a specific problem you are having here?
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  3. #3
    Join Date
    Sep 2007
    Posts
    4

    Default

    Hi Karl,

    Thanks for the response.

    The DataSource is not defined in an application context file at all. For our application it's specified in a configuration file that's doesn't have anything to do with Spring.

    So what I'm trying to work out is - I want to specify a TransactionManager in the spring application context file, but not the DataSource. The DataSource can be accessed programatically.

    So in my case I subclassed DataSourceTransactionManager, in its constructor it can programatically get the DataSource and call setDataSource. What I get is an instance of BasicDataSource. As far as I can tell its the same type of object that Spring would have when it calls setDataSource. In the code, I couldn't see that Spring does anything more with the instance of the DataSource before it calls setDataSource().

    This doesn't work - no exceptions or anything, but no transactions. I've been looking at the Spring source code trying to figure out what I can do.

    Essentially we configure our DataSource in a config file that isn't related to Spring, we don't want to have to specify it twice.

    Thanks for any help,
    Michael

  4. #4
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    So your saying if you duplicate the configuration, your transaction do work? How do you access your dataSource programmatically, there's a good chance you can do the same thing in the configuration or write bean to do this and inject it into the transactionManager.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  5. #5
    Join Date
    Sep 2007
    Posts
    4

    Default

    Karl,

    Yes, if I duplicate the DataSource configuration in the Spring application context file the transactions work fine. Basically I just want to avoid duplicating the configuration. I think the basis of this is - the data source configuration is something the user knows about and configures, but what's in the Spring configuration file is otherwise not user visible - we don't let the user see that file.

    So that sounds right - 'do the same thing in the configuration or write bean' - where would I find that code? I'll look around for it in any case.

    Thanks,
    Michael

  6. #6
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    It really depends on how you can access the existing dataSource. Is it accessible via JNDI? Is it a singleton? How can you access it programmatically?

    If it's JNDI you can use this.
    http://www.springframework.org/docs/...ctoryBean.html

    There's some information here if it's a singleton, 3.2.3.2.2. Instantiation using a static factory method.
    http://www.springframework.org/docs/...ans-definition

    Otherwise you can use a factoryBean to expose the dataSource to Spring.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  7. #7
    Join Date
    Sep 2007
    Posts
    4

    Default

    Karl,

    Ok, thanks. Its basically a singleton, so its something like ConfigModule.getReference().getDataSource(), which returns a BasicDataSource.

    So I would need to subclass or wrap the appropriate Spring code - I'll look at 3.2.3.2.2.

    Thanks very much for the help,

    Michael

  8. #8
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Not a problem, let me know how it goes!
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •