Results 1 to 5 of 5

Thread: AbstractRoutingDataSource not routing when used with Hibernate (sample attached)

  1. #1
    Join Date
    Jul 2005
    Location
    Austin, TX
    Posts
    137

    Question AbstractRoutingDataSource not routing when used with Hibernate (sample attached)

    I'm trying to do some simple physical sharding using AbstractRoutingDataSource along with Hibernate. I don't want to use Hibernate Shards, since we want to be able to change ORMs and still shard (I'll be adding other ORMs in a future version of this test).

    I've attached a maven test project that attempts to shard with two empty localhost MySql databases named "bob" & "jack" (make sure jdbc:mysql://localhost:3306/bob & jdbc:mysql://localhost:3306/jack are available via username "root" & password "password" or change dataSource-context.xml to use the right valuesshard-test.zip). I thought it was working, but I realized that AbstractRoutingDataSource#determineCurrentLookupKe y() is not getting called (see the project's ShardedDataSource class), and therefore "jack" is never getting touched, only "bob".

    Right now, I've got database schema generation turned on, but realize it probably wouldn't work across shards & should be off anyway. I've also set AbstractRoutingDataSource's lenientFallback property to true where I'd prefer false, and defaultTargetDataSource to "bob" where I'd prefer there to be no default data source at all, but application context initialization fails when there's no key. I tried a no-op data source class for defaultTargetDataSource, but that didn't work, either.

    Can anyone knowledgeable have a look and see why ShardedDataSource#determineCurrentLookupKey() is not getting called and all calls are being routed to the default/first data source?

    TIA,
    Matthew

  2. #2

    Default did u find the solution

    i am facing the same issue. did u get any solution ?

  3. #3
    Join Date
    Jul 2005
    Location
    Austin, TX
    Posts
    137

    Default

    Quote Originally Posted by springbuddy2012 View Post
    i am facing the same issue. did u get any solution ?
    Unfortunately, no.

  4. #4

    Default

    any idea about alternate solution to make use of multiple datasource & session factory

  5. #5
    Join Date
    Aug 2006
    Posts
    129

    Default interesting

    simple router :
    Code:
    String current = "one";
    	
    	Log logger = LogFactory.getLog(getClass());
    	
    	protected Object determineCurrentLookupKey() {
    		if(current == "one"){
    			current = "two";
    		}else if(current == "two"){
    			current = "three";
    		}else if(current == "three"){
    			current = "one";
    		}
    		logger.info(current);
    		return current;
    	}
    configuration :
    Code:
    <jdbc:embedded-database id="datasource-one" type="H2"/>
    	<jdbc:embedded-database id="datasource-two" type="H2"/>
    	<jdbc:embedded-database id="datasource-three" type="H2"/>
         
        <util:map id="datasource-map">
        	<entry key="one" value-ref="datasource-one"/>
        	<entry key="two" value-ref="datasource-two"/>
        	<entry key="three" value-ref="datasource-three"/>
        </util:map> 
         
        <bean id="datasource-lookup" class="org.springframework.jdbc.datasource.lookup.MapDataSourceLookup"
        	p:dataSources-ref="datasource-map"
        />
        
        <bean id="datasource" class="SimpleDataSourceRouter"
        	p:dataSourceLookup-ref="datasource-lookup"
        	p:targetDataSources-ref="datasource-map"
        />
        
        <bean id="jdbc-template" class="org.springframework.jdbc.core.JdbcTemplate"
        	p:dataSource-ref="datasource"
        />
    test:

    Code:
    @Inject JdbcTemplate template;
    	
    	@Test
    	public void read(){
    		template.execute("select 1");
    		template.execute("select 1");
    		template.execute("select 1");
    	}
    results :
    Code:
    INFO  EmbeddedDatabaseFactory - Creating embedded database 'datasource-one'
     INFO  EmbeddedDatabaseFactory - Creating embedded database 'datasource-two'
     INFO  EmbeddedDatabaseFactory - Creating embedded database 'datasource-three'
     DEBUG JdbcTemplate - Executing SQL statement [select 1]
     DEBUG DataSourceUtils - Fetching JDBC Connection from DataSource
     INFO  ThreadLocalDataSourceRouter - two
     DEBUG SimpleDriverDataSource - Creating new JDBC Driver Connection to [jdbc:h2:mem:datasource-two;DB_CLOSE_DELAY=-1]
     DEBUG DataSourceUtils - Returning JDBC Connection to DataSource
     DEBUG JdbcTemplate - Executing SQL statement [select 1]
     DEBUG DataSourceUtils - Fetching JDBC Connection from DataSource
     INFO  ThreadLocalDataSourceRouter - three
     DEBUG SimpleDriverDataSource - Creating new JDBC Driver Connection to [jdbc:h2:mem:datasource-three;DB_CLOSE_DELAY=-1]
     DEBUG DataSourceUtils - Returning JDBC Connection to DataSource
     DEBUG JdbcTemplate - Executing SQL statement [select 1]
     DEBUG DataSourceUtils - Fetching JDBC Connection from DataSource
     INFO  ThreadLocalDataSourceRouter - one
     DEBUG SimpleDriverDataSource - Creating new JDBC Driver Connection to [jdbc:h2:mem:datasource-one;DB_CLOSE_DELAY=-1]
     DEBUG DataSourceUtils - Returning JDBC Connection to DataSource
     INFO  GenericApplicationContext - Closing org.springframework.context.support.GenericApplicationContext@f01a1e: ; root of context hierarchy
    INFO  DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1b815bfb: defining beans [datasource-one,datasource-two,datasource-three,datasource-map,datasource-lookup,datasource,jdbc-template,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor]; root of factory hierarchy
    DEBUG SimpleDriverDataSource - Creating new JDBC Driver Connection to [jdbc:h2:mem:datasource-three;DB_CLOSE_DELAY=-1]
    DEBUG SimpleDriverDataSource - Creating new JDBC Driver Connection to [jdbc:h2:mem:datasource-two;DB_CLOSE_DELAY=-1]
    DEBUG SimpleDriverDataSource - Creating new JDBC Driver Connection to [jdbc:h2:mem:datasource-one;DB_CLOSE_DELAY=-1]
    i think it works

Tags for this Thread

Posting Permissions

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