Tabnine Logo
DatabaseConnection
Code IndexAdd Tabnine to your IDE (free)

How to use
DatabaseConnection
in
com.j256.ormlite.support

Best Java code snippets using com.j256.ormlite.support.DatabaseConnection (Showing top 20 results out of 315)

origin: magefree/mage

  public void closeDB() {
    try {
      if (dao != null && dao.getConnectionSource() != null) {
        DatabaseConnection conn = dao.getConnectionSource().getReadWriteConnection();
        conn.executeStatement("shutdown compact", 0);
      }
    } catch (SQLException ex) {
      Logger.getLogger(TableRecordRepository.class).error("Error closing table_record repository - ", ex);
    }
  }
}
origin: stackoverflow.com

 DatabaseConnection conn = dao.startThreadConnection();
Savepoint savePoint = null;
try {
  savePoint = conn.setSavePoint(null);
  doInserts(dao);
} finally {
  // commit at the end
  conn.commit(savePoint);
  dao.endThreadConnection(conn);
}
origin: com.expanset.hk2/hk2-persistence-ormlite

/**
 * @param connectionSource {@link ConnectionSource} for transaction.
 * @throws SQLException Connection error.
 */
public OrmliteTransaction(@Nonnull ConnectionSource connectionSource) 
    throws SQLException {
  Validate.notNull(connectionSource, "connectionSource");
  
  this.connectionSource = connectionSource;	
  this.connection = this.connectionSource.getReadWriteConnection();
  
  final boolean saved = this.connectionSource.saveSpecialConnection(connection);	
  if (saved || connectionSource.getDatabaseType().isNestedSavePointsSupported()) {
    if (connection.isAutoCommitSupported()) {
      autoCommitAtStart = connection.isAutoCommit();
      if (autoCommitAtStart) {
        connection.setAutoCommit(false);
      }
    } else {
      autoCommitAtStart = false;
    }
    savepoint = connection.setSavePoint(SAVE_POINT_PREFIX + savepointCounter.incrementAndGet());
  } else {
    savepoint = null;
    autoCommitAtStart = false;
  }        
} 
 
origin: j256/ormlite-core

@Test
public void testCallBatchTasksAutoCommitTrue() throws Exception {
  TableInfo<Foo, String> tableInfo = new TableInfo<Foo, String>(databaseType, Foo.class);
  ConnectionSource connectionSource = createMock(ConnectionSource.class);
  DatabaseConnection connection = createMock(DatabaseConnection.class);
  expect(connectionSource.isSingleConnection("foo")).andReturn(false);
  expect(connectionSource.getReadWriteConnection("foo")).andReturn(connection);
  expect(connectionSource.saveSpecialConnection(connection)).andReturn(false);
  connectionSource.clearSpecialConnection(connection);
  connectionSource.releaseConnection(connection);
  expect(connection.isAutoCommitSupported()).andReturn(true);
  expect(connection.isAutoCommit()).andReturn(true);
  connection.setAutoCommit(false);
  connection.setAutoCommit(true);
  StatementExecutor<Foo, String> statementExec =
      new StatementExecutor<Foo, String>(databaseType, tableInfo, null);
  replay(connectionSource, connection);
  final AtomicBoolean called = new AtomicBoolean(false);
  statementExec.callBatchTasks(connectionSource, new Callable<Void>() {
    @Override
    public Void call() {
      called.set(true);
      return null;
    }
  });
  assertTrue(called.get());
  verify(connectionSource, connection);
}
origin: j256/ormlite-core

@Test
public void testTransactionManagerSavePointNull() throws Exception {
  ConnectionSource connectionSource = createMock(ConnectionSource.class);
  DatabaseConnection conn = createMock(DatabaseConnection.class);
  expect(conn.isAutoCommitSupported()).andReturn(false);
  expect(conn.setSavePoint(isA(String.class))).andReturn(null);
  conn.commit(null);
  expect(connectionSource.getDatabaseType()).andReturn(databaseType);
  expect(connectionSource.getReadWriteConnection(null)).andReturn(conn);
  expect(connectionSource.saveSpecialConnection(conn)).andReturn(true);
  connectionSource.clearSpecialConnection(conn);
  connectionSource.releaseConnection(conn);
  replay(connectionSource, conn);
  TransactionManager tm = new TransactionManager(connectionSource);
  tm.callInTransaction(new Callable<Void>() {
    @Override
    public Void call() {
      return null;
    }
  });
  verify(connectionSource, conn);
}
origin: j256/ormlite-core

/**
 * Return true if the two connections seem to one one connection under the covers.
 */
protected boolean isSingleConnection(DatabaseConnection conn1, DatabaseConnection conn2) throws SQLException {
  // initialize the connections auto-commit flags
  conn1.setAutoCommit(true);
  conn2.setAutoCommit(true);
  try {
    // change conn1's auto-commit to be false
    conn1.setAutoCommit(false);
    if (conn2.isAutoCommit()) {
      // if the 2nd connection's auto-commit is still true then we have multiple connections
      return false;
    } else {
      // if the 2nd connection's auto-commit is also false then we have a single connection
      return true;
    }
  } finally {
    // restore its auto-commit
    conn1.setAutoCommit(true);
  }
}
origin: j256/ormlite-core

@Test
public void testTransactionManagerRollbackNullSavePoint() throws Exception {
  ConnectionSource connectionSource = createMock(ConnectionSource.class);
  DatabaseConnection conn = createMock(DatabaseConnection.class);
  expect(conn.isAutoCommitSupported()).andReturn(false);
  expect(conn.setSavePoint(isA(String.class))).andReturn(null);
  conn.rollback(null);
  expect(connectionSource.getDatabaseType()).andReturn(databaseType);
  expect(connectionSource.getReadWriteConnection(null)).andReturn(conn);
  expect(connectionSource.saveSpecialConnection(conn)).andReturn(true);
  connectionSource.clearSpecialConnection(conn);
  connectionSource.releaseConnection(conn);
  replay(connectionSource, conn);
  TransactionManager tm = new TransactionManager(connectionSource);
  try {
    tm.callInTransaction(new Callable<Void>() {
      @Override
      public Void call() throws Exception {
        throw new SQLException("you better roll back!!");
      }
    });
    fail("expected an exception");
  } catch (SQLException e) {
    // expected
  }
  verify(connectionSource, conn);
}
origin: j256/ormlite-core

@Override
public CompiledStatement compileStatement(String statement, StatementType type, FieldType[] argFieldTypes,
    int resultFlags, boolean cacheStore) throws SQLException {
  if (proxy == null) {
    return null;
  } else {
    return proxy.compileStatement(statement, type, argFieldTypes, resultFlags, cacheStore);
  }
}
origin: j256/ormlite-core

@Override
public void setAutoCommit(boolean autoCommit) throws SQLException {
  if (proxy != null) {
    proxy.setAutoCommit(autoCommit);
  }
}
origin: j256/ormlite-core

@Override
public void commit(Savepoint savePoint) throws SQLException {
  if (proxy != null) {
    proxy.commit(savePoint);
  }
}
origin: j256/ormlite-core

@Override
public int insert(String statement, Object[] args, FieldType[] argfieldTypes, GeneratedKeyHolder keyHolder)
    throws SQLException {
  if (proxy == null) {
    return 0;
  } else {
    return proxy.insert(statement, args, argfieldTypes, keyHolder);
  }
}
origin: j256/ormlite-core

@Override
public boolean isAutoCommit() throws SQLException {
  if (proxy == null) {
    return false;
  } else {
    return proxy.isAutoCommit();
  }
}
origin: j256/ormlite-core

@Test
public void testExecuteStatement() throws Exception {
  DatabaseConnection conn = createMock(DatabaseConnection.class);
  String statement = "select foo from bar";
  int result = 1312321;
  expect(conn.executeStatement(statement, 0)).andReturn(result);
  conn.close();
  DatabaseConnectionProxy proxy = new DatabaseConnectionProxy(conn);
  replay(conn);
  assertEquals(result, proxy.executeStatement(statement, 0));
  proxy.close();
  verify(conn);
}
origin: j256/ormlite-core

@Test
public void testGeneratedIdSequenceLong() throws Exception {
  DatabaseType databaseType = new NeedsSequenceDatabaseType();
  connectionSource.setDatabaseType(databaseType);
  Dao<GeneratedIdLong, Long> dao = createDao(GeneratedIdLong.class, false);
  StatementExecutor<GeneratedIdLong, Long> se = new StatementExecutor<GeneratedIdLong, Long>(databaseType,
      new TableInfo<GeneratedIdLong, Long>(databaseType, GeneratedIdLong.class), dao);
  DatabaseConnection databaseConnection = createMock(DatabaseConnection.class);
  expect(databaseConnection.queryForLong(isA(String.class))).andReturn(1L);
  expect(databaseConnection.insert(isA(String.class), isA(Object[].class), isA(FieldType[].class),
      (GeneratedKeyHolder) isNull())).andReturn(1);
  replay(databaseConnection);
  GeneratedIdLong genIdSeq = new GeneratedIdLong();
  se.create(databaseConnection, genIdSeq, null);
  verify(databaseConnection);
}
origin: j256/ormlite-core

@Test
public void testSetAutoCommit() throws Exception {
  DatabaseConnection conn = createMock(DatabaseConnection.class);
  boolean autoCommit = false;
  conn.setAutoCommit(autoCommit);
  conn.close();
  DatabaseConnectionProxy proxy = new DatabaseConnectionProxy(conn);
  replay(conn);
  proxy.setAutoCommit(autoCommit);
  proxy.close();
  verify(conn);
}
origin: j256/ormlite-core

@Test
public void testIsAutoCommit() throws Exception {
  DatabaseConnection conn = createMock(DatabaseConnection.class);
  boolean autoCommit = false;
  expect(conn.isAutoCommit()).andReturn(autoCommit);
  conn.close();
  DatabaseConnectionProxy proxy = new DatabaseConnectionProxy(conn);
  replay(conn);
  assertEquals(autoCommit, proxy.isAutoCommit());
  proxy.close();
  verify(conn);
}
origin: j256/ormlite-core

@Test
public void testCallBatchTasksAutoCommitFalse() throws Exception {
  TableInfo<Foo, String> tableInfo = new TableInfo<Foo, String>(databaseType, Foo.class);
  ConnectionSource connectionSource = createMock(ConnectionSource.class);
  DatabaseConnection connection = createMock(DatabaseConnection.class);
  expect(connectionSource.isSingleConnection("foo")).andReturn(false);
  expect(connectionSource.getReadWriteConnection("foo")).andReturn(connection);
  expect(connectionSource.saveSpecialConnection(connection)).andReturn(false);
  connectionSource.clearSpecialConnection(connection);
  connectionSource.releaseConnection(connection);
  expect(connection.isAutoCommitSupported()).andReturn(true);
  expect(connection.isAutoCommit()).andReturn(false);
  StatementExecutor<Foo, String> statementExec =
      new StatementExecutor<Foo, String>(databaseType, tableInfo, null);
  replay(connectionSource, connection);
  final AtomicBoolean called = new AtomicBoolean(false);
  statementExec.callBatchTasks(connectionSource, new Callable<Void>() {
    @Override
    public Void call() {
      called.set(true);
      return null;
    }
  });
  assertTrue(called.get());
  verify(connectionSource, connection);
}
origin: j256/ormlite-core

@Override
public void close() throws IOException {
  if (proxy != null) {
    proxy.close();
  }
}
origin: j256/ormlite-core

@Test
public void testSetSavePoint() throws Exception {
  DatabaseConnection conn = createMock(DatabaseConnection.class);
  String name = "savepoint";
  expect(conn.setSavePoint(name)).andReturn(null);
  conn.close();
  DatabaseConnectionProxy proxy = new DatabaseConnectionProxy(conn);
  replay(conn);
  proxy.setSavePoint(name);
  proxy.close();
  verify(conn);
}
origin: j256/ormlite-core

@Test
public void testCompileStatementStringStatementTypeFieldTypeArrayInt() throws Exception {
  DatabaseConnection conn = createMock(DatabaseConnection.class);
  String statement = "select foo from bar";
  StatementType type = StatementType.DELETE;
  int flags = 11253123;
  expect(conn.compileStatement(statement, type, null, flags, false)).andReturn(null);
  conn.close();
  DatabaseConnectionProxy proxy = new DatabaseConnectionProxy(conn);
  replay(conn);
  proxy.compileStatement(statement, type, null, flags, false);
  proxy.close();
  verify(conn);
}
com.j256.ormlite.supportDatabaseConnection

Javadoc

A reduction of the SQL Connection so we can implement its functionality outside of JDBC.

Most used methods

  • executeStatement
    Execute a statement directly on the connection.
  • isAutoCommit
    Return if auto-commit is currently enabled.
  • setAutoCommit
    Set the auto-commit to be on (true) or off (false). Setting auto-commit to true may or may-not cause
  • commit
    Commit all changes since the savepoint was created. If savePoint is null then commit all outstanding
  • compileStatement
    Like compileStatement(String, StatementType, FieldType[]) except the caller can specify the result f
  • setSavePoint
    Start a save point with a certain name. It can be a noop if savepoints are not supported.
  • close
  • closeQuietly
    Close the connection to the database but swallow any exceptions.
  • insert
    Perform a SQL update while with the associated SQL statement, arguments, and types. This will possib
  • isAutoCommitSupported
    Return if auto-commit is supported.
  • queryForLong
    Perform a query whose result should be a single long-integer value.
  • rollback
    Roll back all changes since the savepoint was created. If savePoint is null then roll back all outst
  • queryForLong,
  • rollback,
  • delete,
  • isClosed,
  • isTableExists,
  • queryForOne,
  • update,
  • releaseSavePoint

Popular in Java

  • Updating database using SQL prepared statement
  • putExtra (Intent)
  • setContentView (Activity)
  • compareTo (BigDecimal)
  • PrintWriter (java.io)
    Wraps either an existing OutputStream or an existing Writerand provides convenience methods for prin
  • SocketException (java.net)
    This SocketException may be thrown during socket creation or setting options, and is the superclass
  • URL (java.net)
    A Uniform Resource Locator that identifies the location of an Internet resource as specified by RFC
  • ResourceBundle (java.util)
    ResourceBundle is an abstract class which is the superclass of classes which provide Locale-specifi
  • Stream (java.util.stream)
    A sequence of elements supporting sequential and parallel aggregate operations. The following exampl
  • IsNull (org.hamcrest.core)
    Is the value null?
  • Top PhpStorm plugins
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now