Skip to Content
author's profile photo Former Member
Former Member

How to get connection instance from hybris?

Hi,

We need to get connection instance in our java code. We use MS SQL as a database. And we want to execute own query without FlexibleSearch.

Now we create connection like this:

  Connection con = DriverManager.getConnection(Config.getParameter("db.url"), Config.getParameter("db.username"), Config.getParameter("db.password"));

But we want to use connection from Hybris Platform which Hybris use in FlexibleSearch. How we can do it?

Thanks.

Add a comment
10|10000 characters needed characters exceeded

Related questions

4 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Jul 25, 2014 at 08:56 AM

    try Registry.getCurrentTenant().getDataSource().getConnection()

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jul 25, 2014 at 08:45 AM

    You could lookup dataSource bean and from there borrow a connection. Make sure to return it to the pool after that.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jul 25, 2014 at 08:46 AM

    Firstly, are you sure you really need to do this? What's the use case? This can go horribly wrong if you don't understand the issues 1. you're by passing the cache so you could be creating performance issues 2. if you're update items then the cache will probably have state entries 3. You have to take care of the joins to all the related tables yourself, it's not easy to understand the internal tables and columns that are used behind the scenes by FlexibleSearch

    If you really need to do this then look at the DefaultStockDao for an example. A JdbcTemplate is wired in with a hybris datasource object e.g.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jul 25, 2014 at 08:49 AM

    Wire to you spring bean a jdbcTemplate : but really do this under only the most extreme circumstance. You will be bypassing platform caching functionality etc.

             <property name="jdbcTemplate">
                 <bean class="org.springframework.jdbc.core.JdbcTemplate" scope="tenant">
                     <property name="dataSource" ref="dataSource" />
                 </bean>
             </property>
    
    
    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.