简体   繁体   中英

How can I register my custom sql driver in JPA configurations of my Spring Boot application?

Currently, I have created a kind of wrapper class for java.sql.Driver and I have implemented most of the common interfaces of java.sql because I have to store some meta data through it. I registered my driver class using this registerDriver method

KDriver driverInst = new KDriver(url, user, password);
DriverManager.registerDriver(driverInst);

It's working fine and I am able to make actual db method calls inside the overiden methods

for example - I have override connect method of sql.driver and returning my instance instead of actual one.

 @Override
public Connection connect(String _url, Properties info) throws SQLException {
    System.out.println("HI THERE Mocked!");
    wrappedDriver = new Driver();
    _connection = wrappedDriver.connect(_url, info);
    Connection kobj = new KConnection(_connection);
    return kobj;
}

Similarly, I have done for other sql interfaces too. Now I want to use this driver class inside my actual application which is using Spring Boot JPA which looks like this

@Entity
@Table(name = "employees")
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(name = "first_name")
    private String firstName;
    @Column(name = "last_name")
    private String lastName;
    @Column(name = "email")
    private String email;
    @Column(name = "timestamp")
    private long timestamp;

    public Employee() {
        super();
    }

    public Employee(String firstName, String lastName, String email, long timestamp) {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.timestamp = timestamp;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }
}

So how can I register my driver class so that my this spring application uses custom driver internally? My application.properties file config is as follows

spring.datasource.url=jdbc:postgresql://localhost:5438/postgres
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.show-sql=true
spring.datasource.driverClassName=com.example.ksql.MyDriver


## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect

#Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update

server.port=8080

PS: Please ask if you have any queries as I may have missed out some point in explaining:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM