Package com.j256.ormlite.support

Examples of com.j256.ormlite.support.DatabaseConnection


  @Test
  public void testComboIndex() throws Exception {
    final ConnectionSource connectionSource = createMock(ConnectionSource.class);
    expect(connectionSource.getDatabaseType()).andReturn(databaseType).anyTimes();
    DatabaseConnection conn = createMock(DatabaseConnection.class);
    expect(connectionSource.getReadWriteConnection()).andReturn(conn);
    final CompiledStatement stmt = createMock(CompiledStatement.class);
    expect(conn.compileStatement(isA(String.class), isA(StatementType.class), isA(FieldType[].class))).andAnswer(
        new IAnswer<CompiledStatement>() {
          private int stmtC = 0;
          public CompiledStatement answer() throws Throwable {
            Object[] args = EasyMock.getCurrentArguments();
            assertNotNull(args);
View Full Code Here


    testStatement(connectionSource, databaseType, sb.toString(), null, rowN, throwExecute, callable);
  }

  private void testStatement(ConnectionSource connectionSource, DatabaseType databaseType, String statement,
      String queryAfter, int rowN, boolean throwExecute, Callable<Integer> callable) throws Exception {
    DatabaseConnection conn = createMock(DatabaseConnection.class);
    CompiledStatement stmt = createMock(CompiledStatement.class);
    DatabaseResults results = null;
    final AtomicInteger rowC = new AtomicInteger(1);
    if (throwExecute) {
      expect(conn.compileStatement(isA(String.class), isA(StatementType.class), isA(FieldType[].class))).andThrow(
          new SQLException("you asked us to!!"));
    } else {
      expect(conn.compileStatement(isA(String.class), isA(StatementType.class), isA(FieldType[].class))).andReturn(
          stmt);
      expect(stmt.runUpdate()).andReturn(rowN);
      stmt.close();
      if (queryAfter != null) {
        expect(conn.compileStatement(isA(String.class), isA(StatementType.class), isA(FieldType[].class))).andReturn(
            stmt);
        results = createMock(DatabaseResults.class);
        expect(results.next()).andReturn(false);
        expect(stmt.runQuery()).andReturn(results);
        stmt.close();
View Full Code Here

  }

  @Override
  public DatabaseConnection getReadWriteConnection() throws SQLException {
    checkInitializedSqlException();
    DatabaseConnection conn = getSavedConnection();
    if (conn != null) {
      return conn;
    }
    synchronized (lock) {
      while (connFreeList.size() > 0) {
        // take the first one off of the list
        ConnectionMetaData connMetaData = getFreeConnection();
        if (connMetaData == null) {
          // need to create a new one
        } else if (testBeforeGetFromPool && !testConnection(connMetaData)) {
          // close expired connection
          closeConnectionQuietly(connMetaData);
        } else {
          logger.debug("reusing connection {}", connMetaData);
          return connMetaData.connection;
        }
      }
      // if none in the free list then make a new one
      DatabaseConnection connection = makeConnection(logger);
      openCount++;
      // add it to our connection map
      connectionMap.put(connection, new ConnectionMetaData(connection, maxConnectionAgeMillis));
      int maxInUse = connectionMap.size();
      if (maxInUse > maxEverUsed) {
View Full Code Here

  public DatabaseConnection getReadWriteConnection() throws SQLException {
    if (!initialized) {
      throw new SQLException(getClass().getSimpleName() + ".initialize() was not called");
    }
    DatabaseConnection saved = getSavedConnection();
    if (saved != null) {
      return saved;
    }
    return new JdbcDatabaseConnection(dataSource.getConnection());
  }
View Full Code Here

  public DatabaseConnection getReadWriteConnection(String username, String password) throws SQLException {
    if (!initialized) {
      throw new SQLException(getClass().getSimpleName() + ".initialize() was not called");
    }
    DatabaseConnection saved = getSavedConnection();
    if (saved != null) {
      return saved;
    }
    return new JdbcDatabaseConnection(dataSource.getConnection(username, password));
  }
View Full Code Here

  private final DatabaseType databaseType = new StubDatabaseType();

  @Test(expected = SQLException.class)
  public void testNoIdBuildDelete() throws Exception {
    DatabaseConnection databaseConnection = createMock(DatabaseConnection.class);
    ConnectionSource connectionSource = createMock(ConnectionSource.class);
    expect(connectionSource.getDatabaseType()).andReturn(databaseType).anyTimes();
    replay(connectionSource);
    MappedDeleteCollection.deleteObjects(databaseType,
        new TableInfo<NoId, Void>(connectionSource, null, NoId.class), databaseConnection,
View Full Code Here

    Dao<LocalEnumInt, Object> dao = createDao(clazz, true);
    OurEnum val = OurEnum.SECOND;
    LocalEnumInt foo = new LocalEnumInt();
    foo.ourEnum = val;
    assertEquals(1, dao.create(foo));
    DatabaseConnection conn = connectionSource.getReadOnlyConnection();
    CompiledStatement stmt = null;
    try {
      stmt =
          conn.compileStatement("select * from " + TABLE_NAME, StatementType.SELECT, noFieldTypes,
              DatabaseConnection.DEFAULT_RESULT_FLAGS);
      DatabaseResults results = stmt.runQuery(null);
      assertTrue(results.next());
      assertEquals(
          val.ordinal(),
View Full Code Here

    Dao<LocalEnumString, Object> dao = createDao(LocalEnumString.class, true);
    OurEnum val = OurEnum.SECOND;
    LocalEnumString foo = new LocalEnumString();
    foo.ourEnum = val;
    assertEquals(1, dao.create(foo));
    DatabaseConnection conn = connectionSource.getReadOnlyConnection();
    CompiledStatement stmt = null;
    try {
      stmt =
          conn.compileStatement("select * from " + TABLE_NAME, StatementType.SELECT, noFieldTypes,
              DatabaseConnection.DEFAULT_RESULT_FLAGS);
      DatabaseResults results = stmt.runQuery(null);
      assertTrue(results.next());
      assertEquals(val.toString(),
          DataType.ENUM_STRING.getDataPersister()
View Full Code Here

    Class<LocalString> clazz = LocalString.class;
    Dao<LocalString, Object> dao = createDao(clazz, true);
    LocalString foo = new LocalString();
    foo.string = "not a date format";
    assertEquals(1, dao.create(foo));
    DatabaseConnection conn = connectionSource.getReadOnlyConnection();
    CompiledStatement stmt = null;
    try {
      stmt =
          conn.compileStatement("select * from " + TABLE_NAME, StatementType.SELECT, noFieldTypes,
              DatabaseConnection.DEFAULT_RESULT_FLAGS);
      DatabaseResults results = stmt.runQuery(null);
      assertTrue(results.next());
      int colNum = results.findColumn(STRING_COLUMN);
      DataType.DATE_STRING.getDataPersister().resultToJava(null, results, colNum);
View Full Code Here

  protected <T, ID> void testType(Dao<T, ID> dao, T foo, Class<T> clazz, Object javaVal, Object defaultSqlVal,
      Object sqlArg, String defaultValStr, DataType dataType, String columnName, boolean isValidGeneratedType,
      boolean isAppropriateId, boolean isEscapedValue, boolean isPrimitive, boolean isSelectArgRequired,
      boolean isStreamType, boolean isComparable, boolean isConvertableId) throws Exception {
    DataPersister dataPersister = dataType.getDataPersister();
    DatabaseConnection conn = connectionSource.getReadOnlyConnection();
    CompiledStatement stmt = null;
    try {
      stmt =
          conn.compileStatement("select * from " + TABLE_NAME, StatementType.SELECT, noFieldTypes,
              DatabaseConnection.DEFAULT_RESULT_FLAGS);
      DatabaseResults results = stmt.runQuery(null);
      assertTrue(results.next());
      int colNum = results.findColumn(columnName);
      Field field = clazz.getDeclaredField(columnName);
View Full Code Here

TOP

Related Classes of com.j256.ormlite.support.DatabaseConnection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.