List of usage examples for java.sql Connection setTransactionIsolation
void setTransactionIsolation(int level) throws SQLException;
Connection
object to the one given. From source file:org.apache.sqoop.manager.SqlManager.java
/** * Create a connection to the database; usually used only from within * getConnection(), which enforces a singleton guarantee around the * Connection object.//from w w w . ja v a 2 s.c o m */ protected Connection makeConnection() throws SQLException { Connection connection; String driverClass = getDriverClass(); try { Class.forName(driverClass); } catch (ClassNotFoundException cnfe) { throw new RuntimeException("Could not load db driver class: " + driverClass); } String username = options.getUsername(); String password = options.getPassword(); String connectString = options.getConnectString(); Properties connectionParams = options.getConnectionParams(); if (connectionParams != null && connectionParams.size() > 0) { LOG.debug( "User specified connection params. " + "Using properties specific API for making connection."); Properties props = new Properties(); if (username != null) { props.put("user", username); } if (password != null) { props.put("password", password); } props.putAll(connectionParams); connection = DriverManager.getConnection(connectString, props); } else { LOG.debug("No connection paramenters specified. " + "Using regular API for making connection."); if (username == null) { connection = DriverManager.getConnection(connectString); } else { connection = DriverManager.getConnection(connectString, username, password); } } // We only use this for metadata queries. Loosest semantics are okay. connection.setTransactionIsolation(getMetadataIsolationLevel()); connection.setAutoCommit(false); return connection; }
From source file:net.unicon.mercury.fac.rdbms.RdbmsMessageFactory.java
protected ConnState beginTransaction(Connection conn, boolean serialized) throws SQLException { ConnState rslt = new ConnState(); if (serialized) { rslt.isoLevel = conn.getTransactionIsolation(); conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); }//from w ww . j a v a 2 s. co m rslt.autoCommit = conn.getAutoCommit(); conn.setAutoCommit(false); return rslt; }
From source file:net.unicon.mercury.fac.rdbms.RdbmsMessageFactory.java
protected void cleanupTransactionConnection(Connection conn, ConnState connst, boolean close, boolean serialized) { if (conn == null) { return;/*from www . ja v a 2 s. c o m*/ } try { if (connst != null) { if (serialized) { conn.setTransactionIsolation(connst.isoLevel); } conn.setAutoCommit(connst.autoCommit); } } catch (SQLException ex) { throw new RuntimeException("Error cleaning up DB Connection.", ex); } finally { if (close) { cleanupConnection(conn); } } }
From source file:org.apache.sqoop.manager.OracleManager.java
/** * Create a connection to the database; usually used only from within * getConnection(), which enforces a singleton guarantee around the * Connection object./* w ww .j av a2 s .c om*/ * * Oracle-specific driver uses READ_COMMITTED which is the weakest * semantics Oracle supports. */ protected Connection makeConnection() throws SQLException { Connection connection; String driverClass = getDriverClass(); try { Class.forName(driverClass); } catch (ClassNotFoundException cnfe) { throw new RuntimeException("Could not load db driver class: " + driverClass); } String username = options.getUsername(); String password = options.getPassword(); String connectStr = options.getConnectString(); connection = CACHE.getConnection(connectStr, username); if (null == connection) { // Couldn't pull one from the cache. Get a new one. LOG.debug("Creating a new connection for " + connectStr + ", using username: " + username); Properties connectionParams = options.getConnectionParams(); if (connectionParams != null && connectionParams.size() > 0) { LOG.debug("User specified connection params. " + "Using properties specific API for making connection."); Properties props = new Properties(); if (username != null) { props.put("user", username); } if (password != null) { props.put("password", password); } props.putAll(connectionParams); connection = DriverManager.getConnection(connectStr, props); } else { LOG.debug("No connection paramenters specified. " + "Using regular API for making connection."); if (username == null) { connection = DriverManager.getConnection(connectStr); } else { connection = DriverManager.getConnection(connectStr, username, password); } } } // We only use this for metadata queries. Loosest semantics are okay. connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); // Setting session time zone setSessionTimeZone(connection); return connection; }
From source file:dao.CarryonDaoDb.java
/** * add user stream blobs in carryon./*from ww w.j a va 2 s .co m*/ * @param bsize - stream blob size * @param category - category of this stream blob * @param mimetype - the mime type of this stream blob * @param btitle - stream blob title * @param blob - the stream blob byte array data * @param zoom - the zoom * @param memberId - the member id to whom this blob belongs to * @param usertags - the usertags * @param caption - the caption * @param displayPhotos - photos are published * @throws BaseDaoException If we have a problem interpreting the data or the data is missing or incorrect */ public void addCarryon(long bsize, String category, String mimetype, String btitle, byte[] blob, int zoom, String memberId, String member, String usertags, String caption, boolean displayPhotos) throws BaseDaoException { if (RegexStrUtil.isNull(memberId) || RegexStrUtil.isNull(mimetype) || RegexStrUtil.isNull(btitle) || RegexStrUtil.isNull(member) || (bsize == 0) || (blob == null) || (blob.length == 0) || (zoom < 0) || (zoom > 200) || RegexStrUtil.isNull(category)) { throw new BaseDaoException("params are null"); } int catVal = new Integer(category).intValue(); if (catVal < GlobalConst.categoryMinSize || catVal > GlobalConst.categoryMaxSize) { throw new BaseDaoException("category values are incorrect, " + catVal); } /** * Set the source based on scalability **/ String sourceName = scalabilityManager.getWriteBlobScalability(memberId); ds = scalabilityManager.getSource(sourceName); if (ds == null) { throw new BaseDaoException("ds is null for sourceName = " + sourceName); } Connection conn = null; List entryIdResult = null; try { conn = ds.getConnection(); if (RegexStrUtil.isNull(caption)) { caption = btitle; } if (WebUtil.isSanEnabled()) { byte[] noBlob = { ' ' }; logger.info("isSanEnabled(), not adding blob"); addBlobQuery.run(conn, noBlob, catVal, mimetype, btitle, memberId, bsize, zoom, caption); } else { logger.info("adding blob query"); addBlobQuery.run(conn, blob, catVal, mimetype, btitle, memberId, bsize, zoom, caption); } conn.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); entryIdResult = entryIdQuery.run(conn, memberId); } catch (Exception e) { StringBuffer sb = new StringBuffer( "error occured while executing in Carryon, addBlob(), params (8) including blob category = "); sb.append(category); sb.append(" mimetype= "); sb.append(mimetype); sb.append(" btitle= "); sb.append(btitle); sb.append(" memberId= "); sb.append(memberId); sb.append(" bsize= "); sb.append(bsize); sb.append(" zoom = "); sb.append(zoom); try { if (conn != null) { conn.close(); } } catch (Exception e1) { throw new BaseDaoException(sb.toString(), e1); } throw new BaseDaoException(sb.toString(), e); } try { if (conn != null) { conn.close(); } } catch (Exception e) { throw new BaseDaoException("conn.commit() error for addBlobQuery", e); } /** * Data connection for non partioned table */ sourceName = scalabilityManager.getWriteZeroScalability(); ds = scalabilityManager.getSource(sourceName); if (ds == null) { throw new BaseDaoException("ds is null for sourceName = " + sourceName); } boolean addHitsEntry = false; try { conn = ds.getConnection(); if (entryIdResult != null && entryIdResult.size() > 0) { if ((Photo) entryIdResult.get(0) != null) { String entryId = ((Photo) entryIdResult.get(0)).getValue(DbConstants.ENTRYID); addTagsQuery.run(conn, memberId, usertags, caption, entryId, catVal); /* just add the entries if there are less than 10 entries in the top10 */ if (displayPhotos && (catVal != DbConstants.FILE_CATEGORY_INT)) { List numRowsList = getCarryonHits(); if (numRowsList != null && numRowsList.size() < NUM_ROWS) { addCarryonHitsQuery.run(conn, memberId, entryId, caption, "0"); addHitsEntry = true; } } addRecentQuery.run(conn, memberId, entryId, catVal, btitle, mimetype, bsize, zoom, caption); } } } catch (Exception e) { try { if (conn != null) { conn.close(); } } catch (Exception e1) { throw new BaseDaoException("conn.close() error addTagQuery/addCarryonHitsQuery failed", e1); } throw new BaseDaoException("addTagQuery/addCarryonHitsQuery failed", e); } try { if (conn != null) { conn.close(); } } catch (Exception e) { throw new BaseDaoException("conn.close() error for addTagsQuery/addCarryonHits", e); } if (WebUtil.isSanEnabled()) { try { SanUtils sanUtils = new SanUtils(); sanUtils.addSanFile(member, btitle, SanConstants.sanUserPath, blob); } catch (SanException e) { throw new BaseDaoException("addSanFile, CarryonDaoDb error," + member + " " + btitle + e); } } /** * delete it from cache, get the new list */ Fqn fqn = cacheUtil.fqn(DbConstants.USER_STREAM_BLOB); if (treeCache.exists(fqn, memberId)) { treeCache.remove(fqn, memberId); } if (addHitsEntry) { fqn = cacheUtil.fqn(DbConstants.TOP_CARRYON); if (treeCache.exists(fqn, DbConstants.TOP_CARRYON)) { treeCache.remove(fqn, DbConstants.TOP_CARRYON); } } fqn = cacheUtil.fqn(DbConstants.RECENT_CARRYON); if (treeCache.exists(fqn, DbConstants.RECENT_CARRYON)) { treeCache.remove(fqn, DbConstants.RECENT_CARRYON); } fqn = cacheUtil.fqn(DbConstants.USER_PAGE); if (treeCache.exists(fqn, member)) { treeCache.remove(fqn, member); } fqn = cacheUtil.fqn(DbConstants.USER_STREAM_BLOBS_CAT); StringBuffer buf = new StringBuffer(memberId); buf.append("-"); buf.append(category); String key = buf.toString(); if (treeCache.exists(fqn, key)) { treeCache.remove(fqn, key); } }
From source file:org.enhydra.jdbc.standard.StandardDataSource.java
/** * @param u/*from ww w .j a va 2 s.co m*/ * @param p * @return * @throws java.sql.SQLException */ synchronized public Connection getConnection(String u, String p) throws SQLException { Connection ret = null; // the connection that gets returned Properties prop = new Properties(); if (u != null) { prop.put("user", u); } if (p != null) { prop.put("password", p); } if (driver == null && bundleContext != null) { try { driver = (Driver) bundleContext.getBundle().loadClass(driverName).newInstance(); loadedFromCCL = true; log.debug("StandardDataSource:getConnection a new driver instance is created"); } catch (Exception e) { log.error("Could not load via OSGi: " + driverName); } } if (driver == null && url == null) { // if no explicit url provided // Build URL from serverName, NetworkProtocol etc. } else { // explicit URL provided if (driver == null) { try { driver = (Driver) Class.forName(driverName).newInstance(); loadedFromCCL = false; log.debug("StandardDataSource:getConnection a new driver instance is created"); } catch (Exception e) { try { driver = (Driver) Class .forName(driverName, true, Thread.currentThread().getContextClassLoader()) .newInstance(); loadedFromCCL = true; } catch (Exception e2) { throw new SQLException( "Error trying to load driver: " + driverName + " : " + e2.getMessage()); } } } // commenting out since at least one driver will complain if you // instantiate the driver outside the Driver Manager // (ie. Cloudscape RMI) /* if (!driver.acceptsURL(url)) { log("Driver does not accept url "+url); throw new SQLException("Driver does not accept url "+url); } */ try { if (loadedFromCCL) { ret = driver.connect(url, prop); // Driver creates the connection } else { ret = DriverManager.getConnection(url, prop); // DriverManager creates the connection } int transIsolation = getTransactionIsolation(); if (transIsolation >= 0) { ret.setTransactionIsolation(transIsolation); } log.debug("StandardDataSource:getConnection Connection from DriverManager is returned"); } catch (SQLException e) { throw new SQLException("Cannot get connection for URL " + url + " : " + e.getMessage()); } } return ret; }
From source file:org.wso2.carbon.user.core.jdbc.JDBCUserStoreManager.java
/** * @return//from w w w.ja v a 2s . c om * @throws SQLException * @throws UserStoreException */ protected Connection getDBConnection() throws SQLException, UserStoreException { Connection dbConnection = getJDBCDataSource().getConnection(); dbConnection.setAutoCommit(false); if (dbConnection.getTransactionIsolation() != Connection.TRANSACTION_READ_COMMITTED) { dbConnection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); } return dbConnection; }
From source file:org.wso2.carbon.registry.core.jdbc.dao.JDBCResourceDAO.java
public void fillChildren(CollectionImpl collection, DataAccessManager dataAccessManager) throws RegistryException { if (Transaction.isStarted()) { fillChildren(collection, 0, -1, JDBCDatabaseTransaction.getConnection()); } else {/*from w w w. ja v a 2 s. co m*/ Connection conn = null; boolean transactionSucceeded = false; try { if (!(dataAccessManager instanceof JDBCDataAccessManager)) { String msg = "Failed to fill children. Invalid data access manager."; log.error(msg); throw new RegistryException(msg); } conn = ((JDBCDataAccessManager) dataAccessManager).getDataSource().getConnection(); // If a managed connection already exists, use that instead of a new // connection. JDBCDatabaseTransaction.ManagedRegistryConnection temp = JDBCDatabaseTransaction .getManagedRegistryConnection(conn); if (temp != null) { conn.close(); conn = temp; } if (conn.getTransactionIsolation() != Connection.TRANSACTION_READ_COMMITTED) { conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); } conn.setAutoCommit(false); fillChildren(collection, 0, -1, conn); transactionSucceeded = true; } catch (SQLException e) { String msg = "Failed to get child paths of " + collection.getPath() + ". " + e.getMessage(); log.error(msg, e); throw new RegistryException(msg, e); } finally { if (transactionSucceeded) { try { conn.commit(); } catch (SQLException e) { log.error("Failed to commit the database connection used in " + "getting child paths of the collection " + collection.getPath()); } } else if (conn != null) { try { conn.rollback(); } catch (SQLException e) { log.error("Failed to rollback the database connection used in " + "getting child paths of the collection " + collection.getPath()); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { log.error("Failed to close the database connection opened in " + "getting the child paths of " + collection.getPath(), e); } } } } }
From source file:org.wso2.carbon.registry.core.jdbc.dao.JDBCResourceDAO.java
public String[] getChildren(CollectionImpl collection, int start, int pageLen, DataAccessManager dataAccessManager) throws RegistryException { String[] childPaths = null;/*from ww w . j a v a 2s.c o m*/ if (Transaction.isStarted()) { childPaths = getChildren(collection, start, pageLen, JDBCDatabaseTransaction.getConnection()); } else { Connection conn = null; boolean transactionSucceeded = false; try { if (!(dataAccessManager instanceof JDBCDataAccessManager)) { String msg = "Failed to get children. Invalid data access manager."; log.error(msg); throw new RegistryException(msg); } conn = ((JDBCDataAccessManager) dataAccessManager).getDataSource().getConnection(); // If a managed connection already exists, use that instead of a new // connection. JDBCDatabaseTransaction.ManagedRegistryConnection temp = JDBCDatabaseTransaction .getManagedRegistryConnection(conn); if (temp != null) { conn.close(); conn = temp; } if (conn.getTransactionIsolation() != Connection.TRANSACTION_READ_COMMITTED) { conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); } conn.setAutoCommit(false); childPaths = getChildren(collection, start, pageLen, conn); transactionSucceeded = true; } catch (SQLException e) { String msg = "Failed to get the child paths " + pageLen + " child paths from " + start + " of resource " + collection.getPath() + ". " + e.getMessage(); log.error(msg, e); throw new RegistryException(msg, e); } finally { if (transactionSucceeded) { try { conn.commit(); } catch (SQLException e) { log.error("Failed to commit the database connection used in " + "getting child paths of the collection " + collection.getPath()); } } else if (conn != null) { try { conn.rollback(); } catch (SQLException e) { log.error("Failed to rollback the database connection used in " + "getting child paths of the collection " + collection.getPath()); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { log.error("Failed to close the database connection used in " + "getting child paths of the collection " + collection.getPath()); } } } } return childPaths; }
From source file:org.wso2.carbon.registry.core.jdbc.dao.JDBCResourceDAO.java
public int getChildCount(CollectionImpl collection, DataAccessManager dataAccessManager) throws RegistryException { int childCount = -1; if (Transaction.isStarted()) { childCount = getChildCount(collection, JDBCDatabaseTransaction.getConnection()); } else {//from w w w . j a v a2 s. com Connection conn = null; boolean transactionSucceeded = false; try { if (!(dataAccessManager instanceof JDBCDataAccessManager)) { String msg = "Failed to get child count. Invalid data access manager."; log.error(msg); throw new RegistryException(msg); } conn = ((JDBCDataAccessManager) dataAccessManager).getDataSource().getConnection(); // If a managed connection already exists, use that instead of a new // connection. JDBCDatabaseTransaction.ManagedRegistryConnection temp = JDBCDatabaseTransaction .getManagedRegistryConnection(conn); if (temp != null) { conn.close(); conn = temp; } if (conn.getTransactionIsolation() != Connection.TRANSACTION_READ_COMMITTED) { conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); } conn.setAutoCommit(false); childCount = getChildCount(collection, conn); transactionSucceeded = true; } catch (SQLException e) { String msg = "Failed to get the child count of resource " + collection.getPath() + ". " + e.getMessage(); log.error(msg, e); throw new RegistryException(msg, e); } finally { if (transactionSucceeded) { try { conn.commit(); } catch (SQLException e) { log.error("Failed to commit the database connection used in " + "getting child count of the collection " + collection.getPath()); } } else if (conn != null) { try { conn.rollback(); } catch (SQLException e) { log.error("Failed to rollback the database connection used in " + "getting child count of the collection " + collection.getPath()); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { log.error("Failed to close the database connection used in " + "getting child count of collection " + collection.getPath()); } } } } return childCount; }