涉及懶加載 如何正確的處理懶加載帶來的弊端

1.假如用戶與訂單 一個(gè)用戶可以有多個(gè)訂單 一個(gè)訂單只能屬于一個(gè)用戶。屬于一對多關(guān)系。
所以在進(jìn)行映射時(shí)候,需要設(shè)置為懶加載,當(dāng)查詢用戶時(shí)候不需要查出用戶的所有訂單,但是當(dāng)你需要使用一個(gè)具體功能時(shí),(比如,需要查出某某用戶的所有訂單的時(shí)候) , 就面臨一個(gè)問題,剛才設(shè)置的是懶加載。

出現(xiàn)問題: 因?yàn)槭菓屑虞d,所以用戶的訂單只有在需要使用時(shí)候才會(huì)查出來,在對JSP頁面進(jìn)行渲染之前都沒有查出用戶對應(yīng)的訂單,而當(dāng)需要對JSP頁面進(jìn)行渲染時(shí),此時(shí)需要發(fā)出SQL語句進(jìn)行查詢用戶下面的那些訂單,而此時(shí)hibernate的會(huì)話已經(jīng)關(guān)閉了,事務(wù)環(huán)境也沒有,而用戶的訂單還沒有查出來,就會(huì)報(bào)錯(cuò),運(yùn)行失敗。

解決方案:
1.對hibernate的會(huì)話周期進(jìn)行延長。(缺點(diǎn):當(dāng)高并發(fā)的時(shí)候特別不適用,因?yàn)檠娱L周期是給每一個(gè)帶.do的都延長會(huì)話周期,有些沒必要的也延長了,所以不適用于高并發(fā))
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">

 <!-- 進(jìn)來后尋找index.do -->
 <welcome-file-list>
    <welcome-file>index.do</welcome-file>
 </welcome-file-list> 
 
 

  

<!-- 通過上下文參數(shù)指定Spring的配置文件 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:app.xml</param-value>
</context-param>

<!-- 前端控制器 (這里用過濾器來充當(dāng)) -->

   <servlet>
    <servlet-name>fc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>fc</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

<!-- 所有服務(wù)進(jìn)入過濾器  修改為utf-8 由spring提供此過濾器 -->
<filter>
<filter-name>enc</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
  <param-name>encoding</param-name>
  <param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>enc</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 配置延長會(huì)話周期的配置   將視圖渲染出來后才關(guān)閉會(huì)話  缺點(diǎn):每個(gè).do的都延長會(huì)話周期 特別不精打細(xì)算(應(yīng)用訪問量大 高并發(fā)的使用此方法不合適) 
<filter>
 <filter-name>osiv</filter-name>名字隨意取
 <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>osiv</filter-name>
  <url-pattern>*.do</url-pattern>(因?yàn)榍懊娴那岸慰刂破鲗懙?.do)
</filter-mapping>-->


<!-- 配置創(chuàng)建spring IoC容器的監(jiān)聽器,當(dāng)服務(wù)器已開啟,立即啟動(dòng)IOC容器,服務(wù)器一關(guān)閉立刻關(guān)閉IOC容器 -->

<!-- 在spring-web-context -context loader listener -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

</web-app>

2.在業(yè)務(wù)層去修改 (缺點(diǎn):代碼量增加 優(yōu)點(diǎn):性能優(yōu)越,能勝任高并發(fā))

package com.lin.dang.biz.impl;

import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lin.dang.biz.UserService;
import com.lin.dang.dao.UserDao;
import com.lin.dang.dto.UserLoginDto;
import com.lin.dang.model.User;

@Service
@Transactional
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;

@Override
public boolean login(UserLoginDto user) {

    User temp = userDao.findUserByName(user.getUsername());
    if (temp != null) {
        return temp.getPassword().equals(user.getPassword());
    }
    return false;

}

@Override
public boolean register(User user) {
    return userDao.save(user);
}

@Override
public User getUserDetail(String username) {

    User user = userDao.findUserByName(username);
    if (Hibernate.isInitialized(user)) {
        Hibernate.initialize(user);
    }           
    //假定加載user時(shí)候沒有將用戶的訂單及時(shí)加載出來,假設(shè)使用的默認(rèn)懶加載,此時(shí)是第二種方法。
    return user;
}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容