title: Providing Connection Info at run-time with Hibernate 4
body: <p>Developing a multi-tenant app based on single Db / multi-schema. There's a common schema since there is data shared between tenants and in addition each tenant has a seperate schema for their private data. Authentication data (user id/pwd/tenent id) is kept in common schema which we access during login. Once authenticated, there is a unique userid/password login for each tenant with rights to common schema as well as tenant schema. The "tenant user id/pwd" will be returned as part of a encrypted security token to the client (stateless) and passed back in subsequent web or rest calls. </p>
  
  <p>We're using Spring, Hibernate 4 and C3PO for pooling. After a lot of reading/searches, I believe its possible but have not found any examples. In a subsequent rest call, after extracting the userid/password from the client provided security token, I want to pass these to hibernate to build a connection to the database or use an existing connection with the same userid/password if it exists in the connection pool. Currently we're just wiring the datasource to the entity factory as follows. Very new to java, hibernate, spring etc but have spent hours searching and at this point need to be pointed in the right direction.</p>
  
  <pre><code>&lt;bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
      destroy-method="close"&gt;
      &lt;property name="driverClass" value="${jdbc.connection.driver}" /&gt;
      &lt;property name="jdbcUrl" value="${jdbc.connection.url}" /&gt;
      &lt;property name="user" value="${jdbc.connection.username}" /&gt;
      &lt;property name="password" value="${jdbc.connection.password}" /&gt;
      &lt;property name="minPoolSize" value="1" /&gt;
      &lt;property name="maxPoolSize" value="30" /&gt;
      &lt;property name="acquireIncrement" value="5" /&gt;
      &lt;property name="idleConnectionTestPeriod" value="300" /&gt;
      &lt;property name="maxIdleTime" value="300" /&gt;
      &lt;property name="maxStatements" value="0" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="entityManagerFactory"
      class="org.springframework.o1rm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;property name="persistenceUnitName" value="jobmanagement" /&gt;
      &lt;property name=""&gt;&lt;/property&gt;
  &lt;/bean&gt;
  </code></pre>