How can I get the connection info( like which db ,db address) in a mybatis interceptor( need help)

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

How can I get the connection info( like which db ,db address) in a mybatis interceptor( need help)

gougouzcm


This is my interceptor , I want get the db info (db name and db address ) here
@Intercepts({
@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class })})
public class MybatisPlugin implements Interceptor {

@Override
    public Object intercept(Invocation invocation) throws Throwable {
       // how can I get the db info here
    }



    @Override
    public Object plugin(Object arg0) {
        return Plugin.wrap(arg0, this);
    }

    @Override
    public void setProperties(Properties arg0) {
    }
}

If there is no effective way to get the db info , use reflect can also be okay.



Can anyone help me to resolve this, tks.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How can I get the connection info( like which db ,db address) in a mybatis interceptor( need help)

Guy Rouillier-2
I don't know if this is the easiest way, but by reading the source, I see that from the Invocation class, you can get the MappedStatement (from the args array), and from the MappedStatement you can get the Configuration.  Also, from the Invocation class, you can get the Executor:

    Executor executor = (Executor) invocation.getTarget();

and from the Executor, you can get the Connection:

    Connection connection = executor.getTransaction().getConnection();

From these various classes, you should be able to get any information you want.  For example, the Connection class has both the database name and the database URL.  Digging around in the others may get you the info from the MyBatis database configuration.

--
Guy Rouillier

------ Original Message ------
To: "mybatis-user" <[hidden email]>
Sent: 6/11/2018 2:47:50 AM
Subject: How can I get the connection info( like which db ,db address) in a mybatis interceptor( need help)



This is my interceptor , I want get the db info (db name and db address ) here
@Intercepts({
@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class })})
public class MybatisPlugin implements Interceptor {

@Override
    public Object intercept(Invocation invocation) throws Throwable {
       // how can I get the db info here
    }



    @Override
    public Object plugin(Object arg0) {
        return Plugin.wrap(arg0, this);
    }

    @Override
    public void setProperties(Properties arg0) {
    }
}

If there is no effective way to get the db info , use reflect can also be okay.



Can anyone help me to resolve this, tks.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: How can I get the connection info( like which db ,db address) in a mybatis interceptor( need help)

gougouzcm
I do as what you said , it works. really thanks.But I just found this operation can be  dangerous.For example:

 Executor executor = (Executor) invocation.getTarget();
 Connection connection = executor.getTransaction().getConnection();

The executor I get can be a CachingExecutor ,  cachingExecutor may will not really read from database but from cache.
If from cache ,there is no need to open a connection and I don't know when should I give it back to data source.




2018-06-12 12:11 GMT+08:00 Guy Rouillier <[hidden email]>:
I don't know if this is the easiest way, but by reading the source, I see that from the Invocation class, you can get the MappedStatement (from the args array), and from the MappedStatement you can get the Configuration.  Also, from the Invocation class, you can get the Executor:

    Executor executor = (Executor) invocation.getTarget();

and from the Executor, you can get the Connection:

    Connection connection = executor.getTransaction().getConnection();

From these various classes, you should be able to get any information you want.  For example, the Connection class has both the database name and the database URL.  Digging around in the others may get you the info from the MyBatis database configuration.

--
Guy Rouillier

------ Original Message ------
To: "mybatis-user" <[hidden email]>
Sent: 6/11/2018 2:47:50 AM
Subject: How can I get the connection info( like which db ,db address) in a mybatis interceptor( need help)



This is my interceptor , I want get the db info (db name and db address ) here
@Intercepts({
@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class })})
public class MybatisPlugin implements Interceptor {

@Override
    public Object intercept(Invocation invocation) throws Throwable {
       // how can I get the db info here
    }



    @Override
    public Object plugin(Object arg0) {
        return Plugin.wrap(arg0, this);
    }

    @Override
    public void setProperties(Properties arg0) {
    }
}

If there is no effective way to get the db info , use reflect can also be okay.



Can anyone help me to resolve this, tks.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re[2]: How can I get the connection info( like which db ,db address) in a mybatis interceptor( need help)

Guy Rouillier-2
I've always closed any connection I've retrieved using getConnection().  You should be safe in doing so.  If somewhere else attempts to close the same connections, most DB drivers are smart enough not go get bothered by that.

--
Guy Rouillier

------ Original Message ------
From: "Chenmin Zhou" <[hidden email]>
Sent: 6/12/2018 4:49:34 AM
Subject: Re: How can I get the connection info( like which db ,db address) in a mybatis interceptor( need help)

I do as what you said , it works. really thanks.But I just found this operation can be  dangerous.For example:

 Executor executor = (Executor) invocation.getTarget();
 Connection connection = executor.getTransaction().getConnection();

The executor I get can be a CachingExecutor ,  cachingExecutor may will not really read from database but from cache.
If from cache ,there is no need to open a connection and I don't know when should I give it back to data source.




2018-06-12 12:11 GMT+08:00 Guy Rouillier <[hidden email]>:
I don't know if this is the easiest way, but by reading the source, I see that from the Invocation class, you can get the MappedStatement (from the args array), and from the MappedStatement you can get the Configuration.  Also, from the Invocation class, you can get the Executor:

    Executor executor = (Executor) invocation.getTarget();

and from the Executor, you can get the Connection:

    Connection connection = executor.getTransaction().getConnection();

From these various classes, you should be able to get any information you want.  For example, the Connection class has both the database name and the database URL.  Digging around in the others may get you the info from the MyBatis database configuration.

--
Guy Rouillier

------ Original Message ------
To: "mybatis-user" <[hidden email]>
Sent: 6/11/2018 2:47:50 AM
Subject: How can I get the connection info( like which db ,db address) in a mybatis interceptor( need help)



This is my interceptor , I want get the db info (db name and db address ) here
@Intercepts({
@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class })})
public class MybatisPlugin implements Interceptor {

@Override
    public Object intercept(Invocation invocation) throws Throwable {
       // how can I get the db info here
    }



    @Override
    public Object plugin(Object arg0) {
        return Plugin.wrap(arg0, this);
    }

    @Override
    public void setProperties(Properties arg0) {
    }
}

If there is no effective way to get the db info , use reflect can also be okay.



Can anyone help me to resolve this, tks.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Re[2]: How can I get the connection info( like which db ,db address) in a mybatis interceptor( need help)

gougouzcm
You are right . But I 'am consider the situation that if the connection we got is in the spring transaction . 
 The aspect from spring will commit the connection and return this connection to datasource , If I close the connection in the mybatis interceptor , I'm wrong about this operation cause some other problem.
such as the transaction will not work. 

2018-06-13 13:25 GMT+08:00 Guy Rouillier <[hidden email]>:
I've always closed any connection I've retrieved using getConnection().  You should be safe in doing so.  If somewhere else attempts to close the same connections, most DB drivers are smart enough not go get bothered by that.

--
Guy Rouillier

------ Original Message ------
From: "Chenmin Zhou" <[hidden email]>
Sent: 6/12/2018 4:49:34 AM
Subject: Re: How can I get the connection info( like which db ,db address) in a mybatis interceptor( need help)

I do as what you said , it works. really thanks.But I just found this operation can be  dangerous.For example:

 Executor executor = (Executor) invocation.getTarget();
 Connection connection = executor.getTransaction().getConnection();

The executor I get can be a CachingExecutor ,  cachingExecutor may will not really read from database but from cache.
If from cache ,there is no need to open a connection and I don't know when should I give it back to data source.




2018-06-12 12:11 GMT+08:00 Guy Rouillier <[hidden email]>:
I don't know if this is the easiest way, but by reading the source, I see that from the Invocation class, you can get the MappedStatement (from the args array), and from the MappedStatement you can get the Configuration.  Also, from the Invocation class, you can get the Executor:

    Executor executor = (Executor) invocation.getTarget();

and from the Executor, you can get the Connection:

    Connection connection = executor.getTransaction().getConnection();

From these various classes, you should be able to get any information you want.  For example, the Connection class has both the database name and the database URL.  Digging around in the others may get you the info from the MyBatis database configuration.

--
Guy Rouillier

------ Original Message ------
To: "mybatis-user" <[hidden email]>
Sent: 6/11/2018 2:47:50 AM
Subject: How can I get the connection info( like which db ,db address) in a mybatis interceptor( need help)



This is my interceptor , I want get the db info (db name and db address ) here
@Intercepts({
@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class })})
public class MybatisPlugin implements Interceptor {

@Override
    public Object intercept(Invocation invocation) throws Throwable {
       // how can I get the db info here
    }



    @Override
    public Object plugin(Object arg0) {
        return Plugin.wrap(arg0, this);
    }

    @Override
    public void setProperties(Properties arg0) {
    }
}

If there is no effective way to get the db info , use reflect can also be okay.



Can anyone help me to resolve this, tks.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.