Announcement Announcement Module
Collapse
No announcement yet.
AbstractRoutingDataSource not routing when used with Hibernate (sample attached) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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 valuesAttachment ). 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
    Attached Files

  • #2
    did u find the solution

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

    Comment


    • #3
      Originally posted by springbuddy2012 View Post
      i am facing the same issue. did u get any solution ?
      Unfortunately, no.

      Comment


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

        Comment


        • #5
          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 [email protected]a1e: ; root of context hierarchy
          INFO  DefaultListableBeanFactory - Destroying singletons in org.sp[email protected]815bfb: 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

          Comment

          Working...
          X