1 23 24 package org.apache.slide.store.impl.rdbms; 25 26 import java.sql.Connection ; 27 import java.sql.PreparedStatement ; 28 import java.sql.ResultSet ; 29 import java.sql.SQLException ; 30 31 import org.apache.slide.common.Service; 32 import org.apache.slide.common.ServiceAccessException; 33 import org.apache.slide.macro.ConflictException; 34 import org.apache.slide.util.logger.Logger; 35 36 41 42 public class SQLServerRDBMSAdapter extends StandardRDBMSAdapter implements SequenceAdapter { 43 44 protected static final String LOG_CHANNEL = SQLServerRDBMSAdapter.class.getName(); 45 46 protected static String normalizeSequenceName(String sequenceName) { 47 return sequenceName.replace('-', '_').toUpperCase() + "_SEQ"; 48 } 49 50 public SQLServerRDBMSAdapter(Service service, Logger logger) { 51 super(service, logger); 52 } 53 54 protected ServiceAccessException createException(SQLException e, String uri) { 55 56 switch (e.getErrorCode()) { 57 case 1205 : getLogger().log(e.getErrorCode() + ": Deadlock resolved on " + uri, LOG_CHANNEL, Logger.WARNING); 59 return new ServiceAccessException(service, new ConflictException(uri)); 60 61 case 547 : case 2627 : getLogger().log(e.getErrorCode() + ": Low isolation conflict for " + uri, LOG_CHANNEL, Logger.WARNING); 64 return new ServiceAccessException(service, new ConflictException(uri)); 65 66 default : 67 getLogger().log( 68 "SQL error " + e.getErrorCode() + " on " + uri + ": " + e.getMessage(), 69 LOG_CHANNEL, 70 Logger.ERROR); 71 72 return new ServiceAccessException(service, e); 73 } 74 75 } 76 77 public boolean isSequenceSupported(Connection conn) { 78 return true; 79 } 80 81 public boolean createSequence(Connection conn, String sequenceName) throws ServiceAccessException { 82 83 String query = 85 "CREATE TABLE dbo." 86 + normalizeSequenceName(sequenceName) 87 + " (ID id_type IDENTITY UNIQUE NOT NULL, DUMY bit NOT NULL)"; 88 89 PreparedStatement statement = null; 90 91 try { 92 statement = conn.prepareStatement(query); 93 statement.executeUpdate(); 94 return true; 95 } catch (SQLException e) { 96 throw new ServiceAccessException(service, e); 97 } finally { 98 close(statement); 99 } 100 101 } 102 103 public long nextSequenceValue(Connection conn, String sequenceName) throws ServiceAccessException { 104 String query = "INSERT INTO dbo." + normalizeSequenceName(sequenceName) + " (DUMY) VALUES(1)"; 105 106 String selectQuery = "SELECT @@identity"; 107 108 PreparedStatement statement = null; 109 PreparedStatement selectStatement = null; 110 ResultSet res = null; 111 112 try { 113 statement = conn.prepareStatement(query); 114 statement.executeUpdate(); 115 116 selectStatement = conn.prepareStatement(selectQuery); 117 res = selectStatement.executeQuery(); 118 if (!res.next()) { 119 throw new ServiceAccessException(service, "Could not increment sequence " + sequenceName); 120 } 121 long value = res.getLong(1); 122 return value; 123 } catch (SQLException e) { 124 throw new ServiceAccessException(service, e); 125 } finally { 126 close(statement); 127 close(selectStatement, res); 128 } 129 } 130 131 public boolean sequenceExists(Connection conn, String sequenceName) throws ServiceAccessException { 132 String selectQuery = getExistsQuery(sequenceName); 133 134 PreparedStatement selectStatement = null; 135 ResultSet res = null; 136 137 try { 138 selectStatement = conn.prepareStatement(selectQuery); 139 res = selectStatement.executeQuery(); 140 return res.next(); 141 } catch (SQLException e) { 142 throw new ServiceAccessException(service, e); 143 } finally { 144 close(selectStatement, res); 145 } 146 } 147 148 protected String getExistsQuery(String sequenceName) { 149 String selectQuery = 150 "SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[" 151 + normalizeSequenceName(sequenceName) 152 + "]') and OBJECTPROPERTY(id, N'IsUserTable') = 1"; 153 return selectQuery; 154 } 155 156 } 157 | Popular Tags |