use of org.dexels.grus.GrusConnection in project navajo by Dexels.
the class GrusProviderImpl method requestConnection.
@Override
public GrusConnection requestConnection(String instance, String name) throws UserException {
DataSource dataSourceInstance = null;
dataSourceInstance = getInstanceDataSource(instance, name);
Map<String, Object> settings = settingsMap.get(dataSourceInstance);
if (settings == null && dataSourceInstance == null) {
settings = defaultSettingsMap.get(name);
if (settings == null) {
throw new UserException(-1, "Could not find settings for tenant-less datasource: " + name + " available (tenant-less) datasources: " + defaultSettingsMap.keySet());
}
dataSourceInstance = defaultDataSources.get(name);
}
int id = connectionCounter.getAndIncrement();
GrusConnection gc;
try {
gc = new GrusDataSource(id, dataSourceInstance, settings, this);
} catch (Exception e) {
throw new UserException(-1, "Could not create datasource connection for: " + instance + " and name: " + name, e);
}
grusIds.put((long) id, gc);
Map<DataSource, Integer> currentMap = userThreadLocal.get();
if (currentMap == null) {
currentMap = new HashMap<>();
}
currentMap.put(dataSourceInstance, id);
userThreadLocal.set(currentMap);
return gc;
}
use of org.dexels.grus.GrusConnection in project navajo by Dexels.
the class SQLMap method createConnection.
protected final void createConnection() throws SQLException, UserException {
if (this.debug) {
Access.writeToConsole(myAccess, this.getClass() + ": in createConnection()\n");
}
if (transactionContext != -1) {
GrusConnection gc = null;
if (GrusProviderFactory.getInstance() != null) {
gc = GrusProviderFactory.getInstance().requestConnection(transactionContext);
} else {
gc = LegacyDbConnectionBroker.getGrusConnection(transactionContext);
}
if (gc == null) {
throw new UserException(-1, "Invalid transaction context set: " + transactionContext);
}
con = gc.getConnection();
if (con == null) {
throw new UserException(-1, "Invalid transaction context set: " + transactionContext);
}
// Set myConnectionBroker.
myConnectionBroker = gc.getMyBroker();
// Make sure to set connection id.
this.connectionId = transactionContext;
}
if (con == null) {
if (this.debug) {
Access.writeToConsole(myAccess, "in createConnection() for datasource " + datasource + " and username " + username + "\n");
}
if (GrusProviderFactory.getInstance() != null) {
// in multitenant or OSGi
if (transactionContext != -1) {
gc = GrusProviderFactory.getInstance().requestConnection(transactionContext);
this.ownConnection = false;
} else {
String instance = this.instance;
if (myAccess != null) {
instance = myAccess.getTenant();
}
if (GrusProviderFactory.getInstance().threadContainsConnection(instance, datasource)) {
logger.debug("Opening yet another connection {} for {} in the same thread!", datasource, instance);
// gc = GrusProviderFactory.getInstance().requestExistingConnection(instance, datasource);
// this.ownConnection = false;
// transactionContext = ((Long) gc.getId()).intValue();
}
gc = GrusProviderFactory.getInstance().requestConnection(instance, datasource);
this.ownConnection = true;
}
multiTenantGrusConnection = gc;
} else {
myConnectionBroker = null;
if (fixedBroker != null) {
myConnectionBroker = fixedBroker.get(this.datasource, null, null);
}
if (myConnectionBroker == null) {
throw new UserException(-1, "Could not create connection to datasource " + this.datasource + ", using username " + this.username + ", fixedBroker = " + fixedBroker + " for tenant: " + instance);
}
gc = myConnectionBroker.getGrusConnection();
}
if (gc == null) {
AuditLog.log("SQLMap", "Could (still) not connect to database: " + datasource + " (" + this.username + ")" + ", check your connection", Level.SEVERE);
throw new UserException(-1, "Could not connect to database: " + datasource + " (" + this.username + ")" + ", check your connection");
}
con = gc.getConnection();
if (con == null) {
AuditLog.log("SQLMap", "Could (still) not connect to database: " + datasource + " (" + this.username + ")" + ", check your connection", Level.SEVERE);
throw new UserException(-1, "Could not connect to database: " + datasource + " (" + this.username + ")" + ", check your connection");
// }
} else {
if (this.debug) {
Access.writeToConsole(myAccess, this.getClass() + ": returned a good connection from the broker manager\n");
}
}
// Set current schema if username was specified...
if (this.alternativeUsername != null) {
// Only works for Oracle...
try {
// Now set current_schema...
PreparedStatement stmt = null;
if (SQLMapConstants.POSTGRESDB.equals(this.getDbIdentifier()) || SQLMapConstants.ENTERPRISEDB.equals(this.getDbIdentifier())) {
stmt = con.prepareStatement("SET SEARCH_PATH TO " + this.alternativeUsername + ",public,oracle");
} else {
stmt = con.prepareStatement("ALTER SESSION SET CURRENT_SCHEMA = " + this.alternativeUsername);
}
stmt.executeUpdate();
stmt.close();
} catch (Exception e) {
logger.error("Looking for schema based on username: " + this.alternativeUsername, e);
throw new UserException(-1, "Switching to schema based on username " + this.alternativeUsername + " failed");
}
}
if (con != null && (myConnectionBroker == null || myConnectionBroker.hasAutoCommit())) {
con.setAutoCommit(autoCommit);
if (!con.getAutoCommit()) {
con.commit();
}
// con.setHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT);
if (transactionIsolation != -1) {
con.setTransactionIsolation(transactionIsolation);
}
// Set session identification.
// SessionIdentification.setSessionId(this.getMetaData() != null ? this.getMetaData().getVendor(): "Unknown", con, this.myAccess);
}
if (this.con != null) {
this.connectionId = (int) gc.getId();
if (this.debug) {
Access.writeToConsole(myAccess, this.getClass() + ": put connection no. " + this.connectionId + " into the connection map\n");
}
}
}
}
Aggregations