MyBatis CDI 1.0 - DAO Pattern w/o @Mappers

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

MyBatis CDI 1.0 - DAO Pattern w/o @Mappers

Homero Villarreal

Hi guys. Is it possible to reuse an injected SqlSession in one or more DAOS?


Frameworks & technologies used

  • Apache Tomcat 8.5
  • Weld w/ CDI 2.0
  • javax.faces-2.3.2
  • mybatis-3.4.5
  • mybatis-cdi-1.0.0

Note: This is not a full JEE, this project does not used EJB nor JTA, etc.


Considerations

  • For now I marked all the DAOS with @RequestScoped for testing purposes in order to inject them in a controller. These DAOs will be injected in the service classes.
  • We do not use @Mappers classes. All the selects, inserts, deletes, calls to stored procedures are in mappers.xml

Suppose we have the following code


DAO 1

@RequestScoped
public class UserDAO {
    private static final Logger logger = LoggerFactory.getLogger(UserDAO.class);

    @Inject
    private SqlSession session;

    public void insert(User user) {
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("use_id", user.getId());
           
            session.insert("my.namespace.com.user.insert", params);
        } catch (Exception e) {
            logger.error("", e);
        }
    }
}


DAO 2


@RequestScoped
public class EmployeeDAO {
    private static final Logger logger = LoggerFactory.getLogger(EmployeeDAO.class);

    @Inject
    private SqlSession session;

    public void update(Employee employee) {
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("employee_id", employee.getId());
           
            session.update("my.namespace.com.employee.update", params);
        } catch (Exception e) {
            logger.error("", e);
        }
    }
}


UI Controller


Both DAOS are injected in the controller.


@Named
@RequestScoped
public class MyController implements Serializable {
    . . .

    @Inject
    private EmployeeDAO employeeDAO;

    @Inject
    private UserDAO userDAO;

    @Transactional
    public void doWork() {
        employeeDAO.update(employee);
        userDAO.insert(user);
    }
   
    . . .
}


However, both method are executed in two different “sessions”.


Method update
JdbcTransaction::openConnection(137) - Opening JDBC Connection
PooledDataSource::popConnection(406) - Created connection 1860179418.
JdbcTransaction::setDesiredAutoCommit(101) - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@6ee015da]
.. rest ...
JdbcTransaction::resetAutoCommit(123) - Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@6ee015da]
JdbcTransaction::close(91) - Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@6ee015da]
PooledDataSource::pushConnection(363) - Returned connection 1860179418 to pool.

Method insert
JdbcTransaction::openConnection(137) - Opening JDBC Connection
PooledDataSource::popConnection(398) - Checked out connection 1860179418 from pool.
JdbcTransaction::setDesiredAutoCommit(101) - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@6ee015da]
JdbcTransaction::resetAutoCommit(123) - Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@6ee015da]
JdbcTransaction::close(91) - Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@6ee015da]
PooledDataSource::pushConnection(363) - Returned connection 1860179418 to pool.



How can I make this work or should I use something else?


Thanks!


--
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: MyBatis CDI 1.0 - DAO Pattern w/o @Mappers

Frank Martínez
Hi Homero,

How are you producing the SqlSession?

On Wed, Sep 6, 2017 at 5:24 PM, Homero Villarreal <[hidden email]> wrote:

Hi guys. Is it possible to reuse an injected SqlSession in one or more DAOS?


Frameworks & technologies used

  • Apache Tomcat 8.5
  • Weld w/ CDI 2.0
  • javax.faces-2.3.2
  • mybatis-3.4.5
  • mybatis-cdi-1.0.0

Note: This is not a full JEE, this project does not used EJB nor JTA, etc.


Considerations

  • For now I marked all the DAOS with @RequestScoped for testing purposes in order to inject them in a controller. These DAOs will be injected in the service classes.
  • We do not use @Mappers classes. All the selects, inserts, deletes, calls to stored procedures are in mappers.xml

Suppose we have the following code


DAO 1

@RequestScoped
public class UserDAO {
    private static final Logger logger = LoggerFactory.getLogger(UserDAO.class);

    @Inject
    private SqlSession session;

    public void insert(User user) {
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("use_id", user.getId());
           
            session.insert("my.namespace.com.user.insert", params);
        } catch (Exception e) {
            logger.error("", e);
        }
    }
}


DAO 2


@RequestScoped
public class EmployeeDAO {
    private static final Logger logger = LoggerFactory.getLogger(EmployeeDAO.class);

    @Inject
    private SqlSession session;

    public void update(Employee employee) {
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("employee_id", employee.getId());
           
            session.update("my.namespace.com.employee.update", params);
        } catch (Exception e) {
            logger.error("", e);
        }
    }
}


UI Controller


Both DAOS are injected in the controller.


@Named
@RequestScoped
public class MyController implements Serializable {
    . . .

    @Inject
    private EmployeeDAO employeeDAO;

    @Inject
    private UserDAO userDAO;

    @Transactional
    public void doWork() {
        employeeDAO.update(employee);
        userDAO.insert(user);
    }
   
    . . .
}


However, both method are executed in two different “sessions”.


Method update
JdbcTransaction::openConnection(137) - Opening JDBC Connection
PooledDataSource::popConnection(406) - Created connection 1860179418.
JdbcTransaction::setDesiredAutoCommit(101) - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@6ee015da]
.. rest ...
JdbcTransaction::resetAutoCommit(123) - Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@6ee015da]
JdbcTransaction::close(91) - Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@6ee015da]
PooledDataSource::pushConnection(363) - Returned connection 1860179418 to pool.

Method insert
JdbcTransaction::openConnection(137) - Opening JDBC Connection
PooledDataSource::popConnection(398) - Checked out connection 1860179418 from pool.
JdbcTransaction::setDesiredAutoCommit(101) - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@6ee015da]
JdbcTransaction::resetAutoCommit(123) - Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@6ee015da]
JdbcTransaction::close(91) - Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@6ee015da]
PooledDataSource::pushConnection(363) - Returned connection 1860179418 to pool.



How can I make this work or should I use something else?


Thanks!


--
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.



--
Frank D. Martínez M.

--
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: MyBatis CDI 1.0 - DAO Pattern w/o @Mappers

Homero Villarreal
I made a mistake, I was calling the transactional method directly and not by a proxy object. So I moved the transactional method to an injected proxy and it worked correctly.

Thanks for you time!

On Thursday, September 7, 2017 at 9:00:47 AM UTC-6, Frank Martinez wrote:
Hi Homero,

How are you producing the SqlSession?

On Wed, Sep 6, 2017 at 5:24 PM, Homero Villarreal <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="6vVki2ajAQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">akte...@...> wrote:

Hi guys. Is it possible to reuse an injected SqlSession in one or more DAOS?


Frameworks & technologies used

  • Apache Tomcat 8.5
  • Weld w/ CDI 2.0
  • javax.faces-2.3.2
  • mybatis-3.4.5
  • mybatis-cdi-1.0.0

Note: This is not a full JEE, this project does not used EJB nor JTA, etc.


Considerations

  • For now I marked all the DAOS with @RequestScoped for testing purposes in order to inject them in a controller. These DAOs will be injected in the service classes.
  • We do not use @Mappers classes. All the selects, inserts, deletes, calls to stored procedures are in mappers.xml

Suppose we have the following code


DAO 1

@RequestScoped
public class UserDAO {
    private static final Logger logger = LoggerFactory.getLogger(UserDAO.class);

    @Inject
    private SqlSession session;

    public void insert(User user) {
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("use_id", user.getId());
           
            session.insert("my.namespace.com.user.insert", params);
        } catch (Exception e) {
            logger.error("", e);
        }
    }
}


DAO 2


@RequestScoped
public class EmployeeDAO {
    private static final Logger logger = LoggerFactory.getLogger(EmployeeDAO.class);

    @Inject
    private SqlSession session;

    public void update(Employee employee) {
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("employee_id", employee.getId());
           
            session.update("my.namespace.com.employee.update", params);
        } catch (Exception e) {
            logger.error("", e);
        }
    }
}


UI Controller


Both DAOS are injected in the controller.


@Named
@RequestScoped
public class MyController implements Serializable {
    . . .

    @Inject
    private EmployeeDAO employeeDAO;

    @Inject
    private UserDAO userDAO;

    @Transactional
    public void doWork() {
        employeeDAO.update(employee);
        userDAO.insert(user);
    }
   
    . . .
}


However, both method are executed in two different “sessions”.


Method update
JdbcTransaction::openConnection(137) - Opening JDBC Connection
PooledDataSource::popConnection(406) - Created connection 1860179418.
JdbcTransaction::setDesiredAutoCommit(101) - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@6ee015da]
.. rest ...
JdbcTransaction::resetAutoCommit(123) - Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@6ee015da]
JdbcTransaction::close(91) - Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@6ee015da]
PooledDataSource::pushConnection(363) - Returned connection 1860179418 to pool.

Method insert
JdbcTransaction::openConnection(137) - Opening JDBC Connection
PooledDataSource::popConnection(398) - Checked out connection 1860179418 from pool.
JdbcTransaction::setDesiredAutoCommit(101) - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@6ee015da]
JdbcTransaction::resetAutoCommit(123) - Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@6ee015da]
JdbcTransaction::close(91) - Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@6ee015da]
PooledDataSource::pushConnection(363) - Returned connection 1860179418 to pool.



How can I make this work or should I use something else?


Thanks!


--
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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="6vVki2ajAQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">mybatis-user...@googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.



--
Frank D. Martínez M.

--
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: MyBatis CDI 1.0 - DAO Pattern w/o @Mappers

Homero Villarreal
In reply to this post by Frank Martínez
Hi Frank,

I made a mistake, I was calling the transactional method directly and not by a proxy object. So I moved the transactional method to an injected proxy and it worked correctly.

Thanks for you time!

On Thursday, September 7, 2017 at 9:00:47 AM UTC-6, Frank Martinez wrote:
Hi Homero,

How are you producing the SqlSession?

On Wed, Sep 6, 2017 at 5:24 PM, Homero Villarreal <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="6vVki2ajAQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">akte...@...> wrote:

Hi guys. Is it possible to reuse an injected SqlSession in one or more DAOS?


Frameworks & technologies used

  • Apache Tomcat 8.5
  • Weld w/ CDI 2.0
  • javax.faces-2.3.2
  • mybatis-3.4.5
  • mybatis-cdi-1.0.0

Note: This is not a full JEE, this project does not used EJB nor JTA, etc.


Considerations

  • For now I marked all the DAOS with @RequestScoped for testing purposes in order to inject them in a controller. These DAOs will be injected in the service classes.
  • We do not use @Mappers classes. All the selects, inserts, deletes, calls to stored procedures are in mappers.xml

Suppose we have the following code


DAO 1

@RequestScoped
public class UserDAO {
    private static final Logger logger = LoggerFactory.getLogger(UserDAO.class);

    @Inject
    private SqlSession session;

    public void insert(User user) {
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("use_id", user.getId());
           
            session.insert("my.namespace.com.user.insert", params);
        } catch (Exception e) {
            logger.error("", e);
        }
    }
}


DAO 2


@RequestScoped
public class EmployeeDAO {
    private static final Logger logger = LoggerFactory.getLogger(EmployeeDAO.class);

    @Inject
    private SqlSession session;

    public void update(Employee employee) {
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("employee_id", employee.getId());
           
            session.update("my.namespace.com.employee.update", params);
        } catch (Exception e) {
            logger.error("", e);
        }
    }
}


UI Controller


Both DAOS are injected in the controller.


@Named
@RequestScoped
public class MyController implements Serializable {
    . . .

    @Inject
    private EmployeeDAO employeeDAO;

    @Inject
    private UserDAO userDAO;

    @Transactional
    public void doWork() {
        employeeDAO.update(employee);
        userDAO.insert(user);
    }
   
    . . .
}


However, both method are executed in two different “sessions”.


Method update
JdbcTransaction::openConnection(137) - Opening JDBC Connection
PooledDataSource::popConnection(406) - Created connection 1860179418.
JdbcTransaction::setDesiredAutoCommit(101) - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@6ee015da]
.. rest ...
JdbcTransaction::resetAutoCommit(123) - Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@6ee015da]
JdbcTransaction::close(91) - Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@6ee015da]
PooledDataSource::pushConnection(363) - Returned connection 1860179418 to pool.

Method insert
JdbcTransaction::openConnection(137) - Opening JDBC Connection
PooledDataSource::popConnection(398) - Checked out connection 1860179418 from pool.
JdbcTransaction::setDesiredAutoCommit(101) - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@6ee015da]
JdbcTransaction::resetAutoCommit(123) - Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@6ee015da]
JdbcTransaction::close(91) - Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@6ee015da]
PooledDataSource::pushConnection(363) - Returned connection 1860179418 to pool.



How can I make this work or should I use something else?


Thanks!


--
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 <a href="javascript:" target="_blank" gdf-obfuscated-mailto="6vVki2ajAQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">mybatis-user...@googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.



--
Frank D. Martínez M.

--
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.