Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  // Copyright 2008 Google Inc.
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  // http://www.apache.org/licenses/LICENSE-2.0
  //
  // Unless required by applicable law or agreed to in writing, software
 // distributed under the License is distributed on an "AS IS" BASIS,
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
 package com.google.gwtorm.jdbc;
 
 
 
Constructor for application com.google.gwtorm.server.Schema extensions.

Applications should use the Database class to create instances of their Schema extension interface, and thus open and connect to the JDBC data store.

Creating a new Database instance is expensive, due to the type analysis and code generation performed to implement the Schema and Access interfaces. Applications should create and cache their Database instance for the live of the application.

Database instances are thread-safe, but returned Schema instances are not.

Parameters:
<T>
 
 public class Database<T extends Schemaimplements SchemaFactory<T> {
   static {
     KeyUtil.setEncoderImpl(new StandardKeyEncoder());
   }
 
   private final DataSource dataSource;
   private final JavaSchemaModel schemaModel;
   private final SchemaFactory<T> implFactory;
   private final SqlDialect implDialect;

  
Create a new database interface, generating the interface implementations.

Parameters:
ds JDBC connection information
schema application extension of the Schema interface to implement.
Throws:
com.google.gwtorm.server.OrmException the schema interface is incorrectly defined, or the driver class is not available through the current class loader.
 
   public Database(final DataSource dsfinal Class<T> schema)
       throws OrmException {
      = ds;
 
     SqlDialect dialect;
     try {
       Connection c = ds.getConnection();
       try {
         dialect = SqlDialect.getDialectFor(c);
       } finally {
         c.close();
       }
     } catch (SQLException e) {
       throw new OrmException("Unable to determine SqlDialect"e);
     }
 
      = new JavaSchemaModel(schema);
     final GeneratedClassLoader loader = newLoader(schema);
     final Class<T> impl = generate(dialectloader);
      = new SchemaConstructorGen<T>(loaderimplthis).create();
      = dialect;
   }
 
   @SuppressWarnings({"unchecked""rawtypes"})
   private Class<T> generate(final SqlDialect dialect,
       final GeneratedClassLoader loaderthrows OrmException {
     return new SchemaGen(loadergetClass(), JdbcSchema.class,
         new SchemaGen.AccessGenerator() {
           @Override
           public Class<?> create(GeneratedClassLoader loaderRelationModel rm)
               throws OrmException {
             return new AccessGen(loaderrmdialect).create();
          }
        }).create();
  }
    return ;
  }
    return ;
  }

  
Open a new connection to the database and get a Schema wrapper.

Returns:
a new JDBC connection, wrapped up in the application's Schema.
Throws:
com.google.gwtorm.server.OrmException the connection could not be opened to the database. The JDBC exception detail should be examined to determine the root cause of the connection failure.
  public T open() throws OrmException {
    return .open();
  }
    final Connection conn;
    try {
      conn = .getConnection();
    } catch (SQLException e) {
      throw new OrmException("Cannot open database connection"e);
    }
    try {
      if (!conn.getAutoCommit()) {
        conn.setAutoCommit(true);
      }
    } catch (SQLException e) {
      try {
        conn.close();
      } catch (SQLException e2) {
      }
      throw new OrmException("Cannot force auto-commit on connection"e);
    }
    return conn;
  }
  private static <T> GeneratedClassLoader newLoader(final Class<T> schema) {
    return new GeneratedClassLoader(schema.getClassLoader());
  }
New to GrepCode? Check out our FAQ X