本文整理汇总了Java中org.springframework.jdbc.support.SQLStateSQLExceptionTranslator类的典型用法代码示例。如果您正苦于以下问题:Java SQLStateSQLExceptionTranslator类的具体用法?Java SQLStateSQLExceptionTranslator怎么用?Java SQLStateSQLExceptionTranslator使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SQLStateSQLExceptionTranslator类属于org.springframework.jdbc.support包,在下文中一共展示了SQLStateSQLExceptionTranslator类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setUp
import org.springframework.jdbc.support.SQLStateSQLExceptionTranslator; //导入依赖的package包/类
@Before
public void setUp() throws SQLException {
connection = mock(Connection.class);
statement = mock(Statement.class);
preparedStatement = mock(PreparedStatement.class);
resultSet = mock(ResultSet.class);
given(connection.createStatement()).willReturn(statement);
given(connection.prepareStatement(anyString())).willReturn(preparedStatement);
given(statement.executeQuery(anyString())).willReturn(resultSet);
given(preparedStatement.executeQuery()).willReturn(resultSet);
given(resultSet.next()).willReturn(true, true, false);
given(resultSet.getString(1)).willReturn("tb1", "tb2");
given(resultSet.getInt(2)).willReturn(1, 2);
template = new JdbcTemplate();
template.setDataSource(new SingleConnectionDataSource(connection, false));
template.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
template.afterPropertiesSet();
}
示例2: Mock
import org.springframework.jdbc.support.SQLStateSQLExceptionTranslator; //导入依赖的package包/类
public Mock(MockType type)
throws Exception {
connection = mock(Connection.class);
statement = mock(Statement.class);
resultSet = mock(ResultSet.class);
resultSetMetaData = mock(ResultSetMetaData.class);
given(connection.createStatement()).willReturn(statement);
given(statement.executeQuery(anyString())).willReturn(resultSet);
given(resultSet.getMetaData()).willReturn(resultSetMetaData);
given(resultSet.next()).willReturn(true, false);
given(resultSet.getString(1)).willReturn("Bubba");
given(resultSet.getLong(2)).willReturn(22L);
given(resultSet.getTimestamp(3)).willReturn(new Timestamp(1221222L));
given(resultSet.getBigDecimal(4)).willReturn(new BigDecimal("1234.56"));
given(resultSet.wasNull()).willReturn(type == MockType.TWO ? true : false);
given(resultSetMetaData.getColumnCount()).willReturn(4);
given(resultSetMetaData.getColumnLabel(1)).willReturn(
type == MockType.THREE ? "Last Name" : "name");
given(resultSetMetaData.getColumnLabel(2)).willReturn("age");
given(resultSetMetaData.getColumnLabel(3)).willReturn("birth_date");
given(resultSetMetaData.getColumnLabel(4)).willReturn("balance");
jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(new SingleConnectionDataSource(connection, false));
jdbcTemplate.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
jdbcTemplate.afterPropertiesSet();
}
示例3: testUseCustomSQLErrorCodeTranslator
import org.springframework.jdbc.support.SQLStateSQLExceptionTranslator; //导入依赖的package包/类
/**
* Test that we see an SQLException translated using Error Code.
* If we provide the SQLExceptionTranslator, we shouldn't use a connection
* to get the metadata
*/
@Test
public void testUseCustomSQLErrorCodeTranslator() throws Exception {
// Bad SQL state
final SQLException sqlException = new SQLException("I have a known problem", "07000", 1054);
final String sql = "SELECT ID FROM CUSTOMER";
given(this.resultSet.next()).willReturn(true);
given(this.connection.createStatement()).willReturn(this.preparedStatement);
JdbcTemplate template = new JdbcTemplate();
template.setDataSource(this.dataSource);
// Set custom exception translator
template.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
template.afterPropertiesSet();
this.thrown.expect(BadSqlGrammarException.class);
this.thrown.expect(exceptionCause(sameInstance(sqlException)));
try {
template.query(sql, new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
throw sqlException;
}
});
}
finally {
verify(this.resultSet).close();
verify(this.preparedStatement).close();
verify(this.connection).close();
}
}
示例4: setUp
import org.springframework.jdbc.support.SQLStateSQLExceptionTranslator; //导入依赖的package包/类
@Before
public void setUp() throws SQLException {
given(connection.createStatement()).willReturn(statement);
given(connection.prepareStatement(anyString())).willReturn(preparedStatement);
given(statement.executeQuery(anyString())).willReturn(resultSet);
given(preparedStatement.executeQuery()).willReturn(resultSet);
given(resultSet.next()).willReturn(true, true, false);
given(resultSet.getString(1)).willReturn("tb1", "tb2");
given(resultSet.getInt(2)).willReturn(1, 2);
template.setDataSource(new SingleConnectionDataSource(connection, false));
template.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
template.afterPropertiesSet();
}
示例5: getTranslator
import org.springframework.jdbc.support.SQLStateSQLExceptionTranslator; //导入依赖的package包/类
private SQLExceptionTranslator getTranslator(ExecuteContext context) {
SQLDialect dialect = context.configuration().dialect();
if (dialect != null && dialect.thirdParty() != null) {
return new SQLErrorCodeSQLExceptionTranslator(
dialect.thirdParty().springDbName());
}
return new SQLStateSQLExceptionTranslator();
}
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:9,代码来源:JooqExceptionTranslator.java
示例6: getTranslator
import org.springframework.jdbc.support.SQLStateSQLExceptionTranslator; //导入依赖的package包/类
private SQLExceptionTranslator getTranslator(ExecuteContext context) {
SQLDialect dialect = context.configuration().dialect();
if (dialect != null) {
return new SQLErrorCodeSQLExceptionTranslator(dialect.name());
}
return new SQLStateSQLExceptionTranslator();
}
示例7: testStoredProcedureConfiguredViaJdbcTemplateWithCustomExceptionTranslator
import org.springframework.jdbc.support.SQLStateSQLExceptionTranslator; //导入依赖的package包/类
/**
* Confirm no connection was used to get metadata. Does not use superclass replay
* mechanism.
*
* @throws Exception
*/
@Test
public void testStoredProcedureConfiguredViaJdbcTemplateWithCustomExceptionTranslator()
throws Exception {
given(callableStatement.execute()).willReturn(false);
given(callableStatement.getUpdateCount()).willReturn(-1);
given(callableStatement.getObject(2)).willReturn(5);
given(connection.prepareCall("{call " + StoredProcedureConfiguredViaJdbcTemplate.SQL + "(?, ?)}")
).willReturn(callableStatement);
class TestJdbcTemplate extends JdbcTemplate {
int calls;
@Override
public Map<String, Object> call(CallableStatementCreator csc,
List<SqlParameter> declaredParameters) throws DataAccessException {
calls++;
return super.call(csc, declaredParameters);
}
}
TestJdbcTemplate t = new TestJdbcTemplate();
t.setDataSource(dataSource);
// Will fail without the following, because we're not able to get a connection
// from the DataSource here if we need to to create an ExceptionTranslator
t.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
StoredProcedureConfiguredViaJdbcTemplate sp = new StoredProcedureConfiguredViaJdbcTemplate(t);
assertEquals(sp.execute(11), 5);
assertEquals(1, t.calls);
verify(callableStatement).setObject(1, 11, Types.INTEGER);
verify(callableStatement).registerOutParameter(2, Types.INTEGER);
}
示例8: testTransactionCommit
import org.springframework.jdbc.support.SQLStateSQLExceptionTranslator; //导入依赖的package包/类
@Test
public void testTransactionCommit() throws Exception {
final DataSource ds = mock(DataSource.class);
Connection con = mock(Connection.class);
final SessionFactory sf = mock(SessionFactory.class);
final Session session = mock(Session.class);
Transaction tx = mock(Transaction.class);
Query query = mock(Query.class);
final List list = new ArrayList();
list.add("test");
given(con.getTransactionIsolation()).willReturn(Connection.TRANSACTION_READ_COMMITTED);
given(sf.openSession()).willReturn(session);
given(session.getTransaction()).willReturn(tx);
given(session.connection()).willReturn(con);
given(session.isOpen()).willReturn(true);
given(session.createQuery("some query string")).willReturn(query);
given(query.list()).willReturn(list);
given(session.isConnected()).willReturn(true);
LocalSessionFactoryBean lsfb = new LocalSessionFactoryBean() {
@Override
protected SessionFactory newSessionFactory(Configuration config) throws HibernateException {
return sf;
}
};
lsfb.afterPropertiesSet();
final SessionFactory sfProxy = lsfb.getObject();
HibernateTransactionManager tm = new HibernateTransactionManager();
tm.setJdbcExceptionTranslator(new SQLStateSQLExceptionTranslator());
tm.setSessionFactory(sfProxy);
tm.setDataSource(ds);
TransactionTemplate tt = new TransactionTemplate(tm);
tt.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
tt.setTimeout(10);
assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sfProxy));
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
Object result = tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sfProxy));
assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(ds));
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
HibernateTemplate ht = new HibernateTemplate(sfProxy);
return ht.find("some query string");
}
});
assertTrue("Correct result list", result == list);
assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sfProxy));
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
verify(con).setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
verify(con).setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
verify(tx).setTimeout(10);
verify(tx).begin();
verify(tx).commit();
verify(session).close();
}
示例9: testTransactionCommitWithEarlyFlush
import org.springframework.jdbc.support.SQLStateSQLExceptionTranslator; //导入依赖的package包/类
@Test
public void testTransactionCommitWithEarlyFlush() throws Exception {
final DataSource ds = mock(DataSource.class);
Connection con = mock(Connection.class);
final SessionFactory sf = mock(SessionFactory.class);
final Session session = mock(Session.class);
Transaction tx = mock(Transaction.class);
Query query = mock(Query.class);
final List list = new ArrayList();
list.add("test");
given(con.getTransactionIsolation()).willReturn(Connection.TRANSACTION_READ_COMMITTED);
given(sf.openSession()).willReturn(session);
given(session.getTransaction()).willReturn(tx);
given(session.connection()).willReturn(con);
given(session.isOpen()).willReturn(true);
given(session.createQuery("some query string")).willReturn(query);
given(query.list()).willReturn(list);
given(session.getFlushMode()).willReturn(FlushMode.AUTO);
given(session.isConnected()).willReturn(true);
LocalSessionFactoryBean lsfb = new LocalSessionFactoryBean() {
@Override
protected SessionFactory newSessionFactory(Configuration config) throws HibernateException {
return sf;
}
};
lsfb.afterPropertiesSet();
final SessionFactory sfProxy = lsfb.getObject();
HibernateTransactionManager tm = new HibernateTransactionManager();
tm.setJdbcExceptionTranslator(new SQLStateSQLExceptionTranslator());
tm.setSessionFactory(sfProxy);
tm.setDataSource(ds);
tm.setEarlyFlushBeforeCommit(true);
TransactionTemplate tt = new TransactionTemplate(tm);
tt.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
tt.setTimeout(10);
assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sfProxy));
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
Object result = tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
assertTrue("Has thread session", TransactionSynchronizationManager.hasResource(sfProxy));
assertTrue("Has thread connection", TransactionSynchronizationManager.hasResource(ds));
assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
HibernateTemplate ht = new HibernateTemplate(sfProxy);
return ht.find("some query string");
}
});
assertTrue("Correct result list", result == list);
assertTrue("Hasn't thread session", !TransactionSynchronizationManager.hasResource(sfProxy));
assertTrue("Hasn't thread connection", !TransactionSynchronizationManager.hasResource(ds));
assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
InOrder ordered = inOrder(con);
ordered.verify(con).setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
ordered.verify(con).setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
verify(tx).setTimeout(10);
verify(tx).begin();
verify(session).flush();
verify(session).setFlushMode(FlushMode.MANUAL);
verify(tx).commit();
verify(session).close();
}
示例10: newJdbcExceptionTranslator
import org.springframework.jdbc.support.SQLStateSQLExceptionTranslator; //导入依赖的package包/类
/**
* Create an appropriate SQLExceptionTranslator for the given PersistenceManagerFactory.
* <p>If a DataSource is found, creates a SQLErrorCodeSQLExceptionTranslator for the
* DataSource; else, falls back to a SQLStateSQLExceptionTranslator.
* @param connectionFactory the connection factory of the PersistenceManagerFactory
* (may be {@code null})
* @return the SQLExceptionTranslator (never {@code null})
* @see javax.jdo.PersistenceManagerFactory#getConnectionFactory()
* @see org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator
* @see org.springframework.jdbc.support.SQLStateSQLExceptionTranslator
*/
static SQLExceptionTranslator newJdbcExceptionTranslator(Object connectionFactory) {
// Check for PersistenceManagerFactory's DataSource.
if (connectionFactory instanceof DataSource) {
return new SQLErrorCodeSQLExceptionTranslator((DataSource) connectionFactory);
}
else {
return new SQLStateSQLExceptionTranslator();
}
}
示例11: newJdbcExceptionTranslator
import org.springframework.jdbc.support.SQLStateSQLExceptionTranslator; //导入依赖的package包/类
/**
* Create an appropriate SQLExceptionTranslator for the given SessionFactory.
* If a DataSource is found, a SQLErrorCodeSQLExceptionTranslator for the DataSource
* is created; else, a SQLStateSQLExceptionTranslator as fallback.
* @param sessionFactory the SessionFactory to create the translator for
* @return the SQLExceptionTranslator
* @see #getDataSource
* @see org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator
* @see org.springframework.jdbc.support.SQLStateSQLExceptionTranslator
*/
public static SQLExceptionTranslator newJdbcExceptionTranslator(SessionFactory sessionFactory) {
DataSource ds = getDataSource(sessionFactory);
if (ds != null) {
return new SQLErrorCodeSQLExceptionTranslator(ds);
}
return new SQLStateSQLExceptionTranslator();
}
示例12: newJdbcExceptionTranslator
import org.springframework.jdbc.support.SQLStateSQLExceptionTranslator; //导入依赖的package包/类
/**
* Create an appropriate SQLExceptionTranslator for the given TransactionManager.
* If a DataSource is found, a SQLErrorCodeSQLExceptionTranslator for the DataSource
* is created; else, a SQLStateSQLExceptionTranslator as fallback.
* @param transactionManager the TransactionManager to create the translator for
* @return the SQLExceptionTranslator
* @see #getDataSource
* @see org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator
* @see org.springframework.jdbc.support.SQLStateSQLExceptionTranslator
*/
public static SQLExceptionTranslator newJdbcExceptionTranslator() {
return new SQLStateSQLExceptionTranslator();
}