cancel
Showing results for 
Search instead for 
Did you mean: 

How to get connection instance from hybris?

Former Member
0 Kudos

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.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

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

Answers (3)

Answers (3)

Former Member
0 Kudos

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>

Former Member
0 Kudos

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.

Former Member
0 Kudos

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