Unverified Commit b2e491ae authored by 张亮's avatar 张亮 Committed by GitHub
Browse files

Merge pull request #59 from tristaZero/dev

Create common repository module
parents 3a212748 2834f283
Loading
Loading
Loading
Loading
+15 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>common-repository</artifactId>
        <groupId>io.shardingsphere</groupId>
        <version>3.0.0.M5-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>jdbc-repository</artifactId>


</project>
 No newline at end of file
+38 −13
Original line number Diff line number Diff line
@@ -15,7 +15,7 @@
 * </p>
 */

package io.shardingsphere.example.proxy.repository;
package io.shardingsphere.example.repository.common.repository;

import javax.sql.DataSource;
import java.sql.Connection;
@@ -24,12 +24,19 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class XaRawJdbcRepository {
public class DataRepository {
    
    private final DataSource dataSource;
    
    public XaRawJdbcRepository(final DataSource dataSource) {
    private final boolean isXA;
    
    public DataRepository(final DataSource dataSource) {
        this(dataSource, false);
    }
    
    public DataRepository(final DataSource dataSource, final boolean isXA) {
        this.dataSource = dataSource;
        this.isXA = isXA;
    }
    
    public void demo() throws SQLException {
@@ -51,7 +58,7 @@ public class XaRawJdbcRepository {
    
    private void insertData() throws SQLException {
        Connection connection = dataSource.getConnection();
        connection.setAutoCommit(false);
        setAutoCommit(connection);
        Statement statement = connection.createStatement();
        try {
            for (int i = 1; i < 10; i++) {
@@ -60,9 +67,9 @@ public class XaRawJdbcRepository {
                orderId = insertAndGetGeneratedKey(statement,"INSERT INTO t_order (user_id, status) VALUES (11, 'INIT')");
                statement.execute(String.format("INSERT INTO t_order_item (order_id, user_id) VALUES (%d, 11)", orderId));
            }
            connection.commit();
            commit(connection);
        } catch (SQLException ex) {
            connection.rollback();
            rollback(connection);
        }
        finally {
            connection.close();
@@ -72,7 +79,7 @@ public class XaRawJdbcRepository {
    
    private void insertFailure() throws SQLException {
        Connection connection = dataSource.getConnection();
        connection.setAutoCommit(false);
        setAutoCommit(connection);
        Statement statement = connection.createStatement();
        try {
            for (int i = 1; i < 10; i++) {
@@ -82,9 +89,9 @@ public class XaRawJdbcRepository {
                statement.execute(String.format("INSERT INTO t_order_item (order_id, user_id) VALUES (%d, 11)", orderId));
            }
            makeException();
            connection.commit();
            commit(connection);
        } catch (Exception ex) {
            connection.rollback();
            rollback(connection);
        }
        finally {
            connection.close();
@@ -107,9 +114,9 @@ public class XaRawJdbcRepository {
        return result;
    }
    
    protected void updateData() throws SQLException {
    private void updateData() throws SQLException {
        Connection connection = dataSource.getConnection();
        connection.setAutoCommit(false);
        setAutoCommit(connection);
        try {
            for (int i = 1; i <= 10; i++) {
                Long orderId = getRandomOrderId(connection, 10);
@@ -119,9 +126,9 @@ public class XaRawJdbcRepository {
                preparedStatement.setObject(2, orderId);
                preparedStatement.executeUpdate();
            }
            connection.commit();
            commit(connection);
        } catch (SQLException ex) {
            connection.rollback();
            rollback(connection);
        }
        finally {
            connection.close();
@@ -141,6 +148,24 @@ public class XaRawJdbcRepository {
        return 0L;
    }
    
    private void setAutoCommit(final Connection connection) throws SQLException {
        if (isXA) {
            connection.setAutoCommit(false);
        }
    }
    
    private void commit(final Connection connection) throws SQLException {
        if (isXA) {
            connection.commit();
        }
    }
    
    private void rollback(final Connection connection) throws SQLException {
        if (isXA) {
            connection.rollback();
        }
    }
    
    private void queryWithEqual() throws SQLException {
        String sql = "SELECT i.* FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id WHERE o.user_id=?";
        try (
+1 −1
Original line number Diff line number Diff line
@@ -15,7 +15,7 @@
 * </p>
 */

package io.shardingsphere.example.jdbc.fixture;
package io.shardingsphere.example.repository.common.repository;

import org.apache.commons.dbcp.BasicDataSource;

+41 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>common-repository</artifactId>
        <groupId>io.shardingsphere</groupId>
        <version>3.0.0.M5-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>jpa-repository</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>${jpa.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring-framework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring-framework.version}</version>
        </dependency>
    </dependencies>
</project>
 No newline at end of file
+1 −1
Original line number Diff line number Diff line
@@ -15,7 +15,7 @@
 * </p>
 */

package io.shardingsphere.example.spring.boot.jpa.entity;
package io.shardingsphere.example.repository.jpa.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
Loading