Compare commits
	
		
			1 Commits 
		
	
	
		
			master
			...
			multitenan
		
	
	| Author | SHA1 | Date | 
|---|---|---|
| 
							
							
								
								 | 
						885e20a400 | 11 years ago | 
@ -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;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -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
 | 
				
			||||||
					Loading…
					
					
				
		Reference in New Issue