Konfigurace pro provoz s více firmami.

multitenant
Josef Rokos 10 years ago
parent 3b4509d0a7
commit 885e20a400

@ -202,6 +202,12 @@
</dependency> </dependency>
<!-- DB --> <!-- DB -->
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901-1.jdbc4</version>
</dependency>
<dependency> <dependency>
<groupId>mysql</groupId> <groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId> <artifactId>mysql-connector-java</artifactId>

@ -48,7 +48,7 @@ public class AppInitListener implements ServletContextListener {
@Override @Override
public void contextInitialized(ServletContextEvent evt) { public void contextInitialized(ServletContextEvent evt) {
Logger logger = LoggerFactory.getLogger(AppInitListener.class); /*Logger logger = LoggerFactory.getLogger(AppInitListener.class);
logger.info("Initializing database"); logger.info("Initializing database");
WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(evt.getServletContext()); WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(evt.getServletContext());
@ -71,7 +71,7 @@ public class AppInitListener implements ServletContextListener {
this.checkGlobalSettings(); this.checkGlobalSettings();
userService.removeAccess(); userService.removeAccess();
loadModuleReports(); loadModuleReports();*/
} }
private void checkMUnits() private void checkMUnits()

@ -0,0 +1,78 @@
package info.bukova.isspst;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.hibernate.HibernateException;
import org.hibernate.service.UnknownUnwrapTypeException;
import org.hibernate.service.jdbc.connections.spi.ConnectionProvider;
import org.hibernate.service.jdbc.connections.spi.MultiTenantConnectionProvider;
public class ClientConnectionPrivider implements MultiTenantConnectionProvider {
/**
*
*/
private static final long serialVersionUID = 2142963179208004018L;
private DataSource dataSource;
public ClientConnectionPrivider(DataSource dataSource) {
this.dataSource = dataSource;
}
@SuppressWarnings("rawtypes")
@Override
public boolean isUnwrappableAs(Class unwrapType) {
return ConnectionProvider.class.equals( unwrapType ) || MultiTenantConnectionProvider.class.equals( unwrapType ) || ClientConnectionPrivider.class.isAssignableFrom( unwrapType );
}
@SuppressWarnings("unchecked")
@Override
public <T> T unwrap(Class<T> unwrapType) {
if ( isUnwrappableAs( unwrapType ) ) {
return (T) this;
}
else {
throw new UnknownUnwrapTypeException( unwrapType );
}
}
@Override
public Connection getAnyConnection() throws SQLException {
return dataSource.getConnection();
}
@Override
public void releaseAnyConnection(Connection connection) throws SQLException {
connection.close();
}
@Override
public Connection getConnection(String tenantIdentifier)
throws SQLException {
Connection con = getAnyConnection();
try {
con.createStatement().execute("SET schema '" + tenantIdentifier + "'");
} catch ( SQLException e ) {
throw new HibernateException("Could not alter JDBC connection to specified schema [" + tenantIdentifier + "]", e);
}
return con;
}
@Override
public void releaseConnection(String tenantIdentifier, Connection connection)
throws SQLException {
releaseAnyConnection(connection);
}
@Override
public boolean supportsAggressiveRelease() {
return false;
}
}

@ -0,0 +1,35 @@
package info.bukova.isspst;
import javax.servlet.http.HttpServletRequest;
import org.hibernate.context.spi.CurrentTenantIdentifierResolver;
import org.springframework.beans.factory.annotation.Autowired;
public class ClientResolver implements CurrentTenantIdentifierResolver {
@Autowired
private HttpServletRequest request;
private static final String TOP_DOMAIN = "localhost"; // Bude se tahat z konfigurace...
@Override
public String resolveCurrentTenantIdentifier() {
String hostName = request.getServerName();
String tenant = "";
if (hostName.contains(".")) {
tenant = hostName.substring(0, hostName.indexOf("." + TOP_DOMAIN));
}
if (tenant.isEmpty()) {
tenant = "public";
}
return tenant;
}
@Override
public boolean validateExistingCurrentSessions() {
return false;
}
}

@ -26,7 +26,7 @@ public class TripRequirement extends RequirementBase {
private String to; private String to;
@Column(name = "TRIP_DATE") @Column(name = "TRIP_DATE")
private Date tripDate; private Date tripDate;
@Column(name = "END") @Column(name = "TRIP_END")
private String end; private String end;
@Column(name = "END_DATE") @Column(name = "END_DATE")
private Date endDate; private Date endDate;

@ -17,7 +17,7 @@ import javax.persistence.Table;
import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetails;
@Entity @Entity
@Table(name="USER") @Table(name="USER_LOGIN")
public class User extends Member implements UserDetails, DataModel { public class User extends Member implements UserDetails, DataModel {
/** /**

@ -1,5 +1,11 @@
jdbc.driverClassName=com.mysql.jdbc.Driver #jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.dialect=org.hibernate.dialect.MySQLDialect #jdbc.dialect=org.hibernate.dialect.MySQLDialect
jdbc.databaseurl=jdbc:mysql://127.0.0.1:3306/isspst?characterEncoding=latin2&autoReconnect=true #jdbc.databaseurl=jdbc:mysql://127.0.0.1:3306/isspst?characterEncoding=latin2&autoReconnect=true
#jdbc.username=isspst
#jdbc.password=xsacfgd
jdbc.driverClassName=org.postgresql.Driver
jdbc.dialect=org.hibernate.dialect.PostgreSQLDialect
jdbc.databaseurl=jdbc:postgresql://127.0.0.1:5432/isspst
jdbc.username=isspst jdbc.username=isspst
jdbc.password=xsacfgd jdbc.password=xsacfgd

@ -50,19 +50,36 @@
<value>classpath:hibernate.cfg.xml</value> <value>classpath:hibernate.cfg.xml</value>
</property> </property>
<property name="hibernateProperties"> <property name="hibernateProperties">
<props> <map>
<prop key="hibernate.dialect">${jdbc.dialect}</prop> <entry key="hibernate.dialect" value="${jdbc.dialect}"/>
<entry key="hibernate.show_sql" value="false"/>
<entry key="hibernate.multiTenancy" value="SCHEMA"/>
<entry key="hibernate.tenant_identifier_resolver" value-ref="clientResolver"/>
<entry key="hibernate.multi_tenant_connection_provider" value-ref="conProvider"/>
</map>
<!-- <props>
<prop key="hibernate.dialect"></prop>
<prop key="hibernate.show_sql">false</prop> <prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.hbm2ddl.auto">update</prop>
<!-- <prop key="hibernate.enable_lazy_load_no_trans">true</prop> --> </props> -->
</props>
</property> </property>
</bean> </bean>
<bean id="clientResolver" class="info.bukova.isspst.ClientResolver" scope="request">
<aop:scoped-proxy/>
</bean>
<bean id="conProvider" class="info.bukova.isspst.ClientConnectionPrivider">
<constructor-arg ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/> <tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager" <bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager"> class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property> <property name="sessionFactory" ref="sessionFactory"/>
<property name="autodetectDataSource" value="false"/>
</bean> </bean>
<!-- Security --> <!-- Security -->

Loading…
Cancel
Save