Coder Social home page Coder Social logo

simpledb's Introduction

simpleDb

1. DbConnectionUtil

Db의 연결을 담당하는 클래스입니다.

public static Connection getConnection(String url, String username, String password) {
        Connection con;

        try {

            con = DriverManager.getConnection(url, username, password);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return con;
    }
    
  public static void closeAll(Connection con, PreparedStatement pstmt, ResultSet rs) {

        closeResultSet(rs);
        closePstmt(pstmt);
        closeConnection(con);

    }

getConnection : 실제 db의 연결을 담당하는 메소드입니다.

closeConnection : closeConnection는 커넥션을 끊는 메소드입니다.

closePstmt : closePstmt는 prepareStatement를 끊는 메소드입니다.

closeResultSet : closeResultSet는 ResultSet을을 끊는 메소드입니다.

2. MyDatasource

데이터풀을 관리하는 클래스입니다.

public synchronized Connection getConnection() {
        long startTime = System.currentTimeMillis();
        while (System.currentTimeMillis() - startTime < time) {
            for (Map.Entry<Connection, Boolean> entry : map.entrySet()) {
                if (entry.getValue()) {
                    Connection connection = entry.getKey();
                    map.put(connection, false);
                    connectionPool--;
                    return connection;
                }
            }
        }

        throw new NoConnection("가능한 커넥션이 없습니다.");
    }
  • map에 미리 커넥션을 저장합니다. 만약 커넥션을 사용중이라면 해당 값을 true로 설정해줍니다.
  • startTime과 **time*을 이용하여 대기시간을 구현하였습니다.
  • 동시성을 방지하기 위해 synchronized를 붙여주었습니다.

3. SimpleDb

데이터베이스와의 연결 및 실행의 중간을 담당하는 클래스입니다.(퍼사드)

  • 퍼사드 패턴으로 연결 및 실행을 담당하는 클래스입니다.

SQL

스크린샷 2023-05-16 115215

4. Sql

sqlinterface입니다. 실제로 sql을 작성하려면 위 interface를 구현하여야 합니다.

5. SqlTransactionAbstract , SqlTransactionInterface

    default T logic(SqlByInterface sql) {
        T result = null;
        try {
            init(sql.getCon());
            result = sqlExecute();
            logging(sql.isDevmode(), sql.getPstmt());
            commit(sql);
        } catch (Exception e) {
            rollback(sql.getCon());
            throw new RuntimeException();
        } finally {
            closeResource(sql);
        }
        return result;
    }

템플릿 메소드 패턴을 사용한 클래스입니다. 위 메소드를 통해서 commit, rollbacklogging과정과 release과정을 구현하였습니다.

6. SqlByAbstract, SqlByInterface

실제 SQL을 받아서 실행하는 클래스입니다. 모든 쿼리는 해당 SqlTransactionAbstract 또는 SqlTransactionInterface를 통해 작성하였습니다.

일부 함수

public long update() {

        SqlTransactionInterface<Long> sql = () -> Long.valueOf(executeUpdate());

        return sql.logic(this);
    }

simpledb's People

Contributors

greentea9227 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.