目前分類:JAVA小筆記 (13)

瀏覽方式: 標題列表 簡短摘要
  • 為了方便取得SessionFactory實例,可撰寫一個test.com.util .HibernateUtil類別:

    package test.com.util;

    import org.hibernate.SessionFactory;

    import org.hibernate.cfg.Configuration;

    public class HibernateUtil {

        private static SessionFactory sessionFactory;

        static {

            try {

                sessionFactory = new Configuration().configure().buildSessionFactory();

            } catch (Throwable ex) {

                throw new ExceptionInInitializerError(ex);

            }

        }

        public static SessionFactory getSessionFactory() {

            return sessionFactory;

        }

        public static void shutdown() {

            getSessionFactory().close();

        }

    }


楊 bey kuen 發表在 痞客邦 留言(0) 人氣()

開始Hibernate:

  • 對於該Table建立一個對應的test.com.hbm.User.hbm.xml:

    <?xml version="1.0" encoding="UTF-8"?>

    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

    <hibernate-mapping>

        <class name="test.com.model.UserModel" table="user">

            <id column="id" name="id" type="java.lang.Integer">

                <generator class="native"/>

            </id>

            <property column="name" name="name" type="java.lang.String"/>

            <property column="age" name="age" type="java.lang.String"/>

        </class>

    </hibernate-mapping>

     

  • 對該test.com.hbm.User.hbm.xml,建立一個test.com.model.UserModel:

楊 bey kuen 發表在 痞客邦 留言(0) 人氣()

說明:

    Hibernate 是「物件/關係對應」ORM(Object/Relational Mapping)的解決方案,簡單說就是物件對應關係,將JAVA中物件映射至關聯式資料庫中的表格, Hibernate 提供了這個過程中自動對應轉換的方案。

楊 bey kuen 發表在 痞客邦 留言(0) 人氣()

  • 下載proxool,下載網址:http://proxool.sourceforge.net/
  • 解壓縮後將其中 proxool-0.9.1.jar、proxool-cglib.jar 放入專案中WEB-INF/libs(依各專案設定)
  • 於 hibernate.cfg.xml 同一層目錄下新增一XML檔 proxool.cfg.xml,檔案內容如下:

<?xml version="1.0" encoding="utf-8"?>
<!-- the proxool configuration can be embedded within your own application's.

楊 bey kuen 發表在 痞客邦 留言(0) 人氣()

目錄:
一、術語session

楊 bey kuen 發表在 痞客邦 留言(0) 人氣()

Apache SSL

一、預備檔案

         1. Apache_2.2.4-win32-openssl-0.9.8d.msi  <*重要的是注意1.2.openssl版本要相符*>

         2. OpenSSL-0.9.8d_win32.rar

         3. openssl.cnf

二、安裝Openssl、產生CA檔案:

        1. OpenSSL-0.9.8d_win32.rar解壓縮,以解壓縮至 C:\Openssl為例,底下應該會有 \bin, \include, \lib, \ssl 四個資料夾和一個 openssl.cnf 檔。

        2. 另外抓取 openssl.cnf(一、3.) 覆蓋掉原本的。

        3. 用文字編輯器打開覆蓋後的 openssl.cnf,修改以下部份:*要注意"\\",不可以少打成"\"

            dir = ssl

            certs = $dir\\certs

            crl_dir = $dir\\crl

            database = $dir\\index.txt

            new_certs_dir = $dir

            certificate = $dir\\cacert.pem

            serial = $dir\\serial

            crl = $dir\\crl.pem

            private_key = $dir\\privkey.pem

            RANDFILE = $dir\\privkey.rnd

        4. x509extensions 開頭的這一行註解掉。(前面加上#字號)

        5. C:\Openssl\ 開啟新資料夾 ssl,並在其中新增 index.txt serial 兩個檔案, serial用文字編輯器打開,輸入01index.txt保持空白

        6. \bin底下的檔案複製一份到 C:\Openssl\底下。(這是為了避免之後路徑的困擾)

        7. 進入命令列模式,依序執行以下動作:

            7-1: openssl genrsa -des3 -out ssl/ca.key 1024

            7-2: openssl req -config openssl.cnf -new -key ssl/ca.key -out ssl/ca.csr

            7-3: openssl x509 -days 3650 -req -signkey ssl/ca.key -in ssl/ca.csr -out ssl/ca.crt

            7-4: openssl genrsa -out ssl/server.key 1024

            7-5: openssl req -config openssl.cnf -new -key ssl/server.key -out ssl/server.csr openssl ca -config

            7-6: openssl.cnf -days 3650 -cert ssl/ca.crt -keyfile ssl/ca.key -in ssl/server.csr -out ssl/server.crt

            *在上列6個步驟裡面會要求輸入密碼、國碼(04)、國家、地區、公司名稱、單位、網站位址、Email

            * 7-6步驟,會有兩個問題[y/n],都要選y.不然產生的server.crt會出錯,變成空檔案(Empty)

            *如果有error多半是路徑錯誤,稍微檢查一下指令做修正即可。

        8. 7產生的檔案(整個ssl資料夾)複製到 C:/Appserv/Apache2.2/conf/ 底下。

 

三、修改httpd-ssl.conf

        1. 打開 C:\AppServ\Apache2.2\conf\extra\httpd-ssl.conf 檔案,修改部份如下:

            DocumentRoot 網頁根目錄,比照httpd.conf下的DocumentRoot

            ServerName Server位址:443

            ServerAdmin Email

            ErrorLog logs/error_log

            TransferLog logs/access_log

            ---

            SSLCertificateFile conf/ssl/server.crt    <------crt存放目錄

            SSLCertificateKeyFile conf/ssl/server.key   <------key存放目錄

            SSLCertificateChainFile conf/ssl/ca.crt   <------中繼憑證存放目錄

            ---

            SSLMutex default註解掉,改為SSLMutex none

            ---

            將所有沒有用雙引號包起來的絕對路徑加上雙引號,例如:

            SSLSessionCache "shmcb:C:/AppServ/Apache2.2/logs/ssl_scache(512000)"

            SSLCertificateFile "C:/AppServ/Apache2.2/conf/ssl/server.crt"

            ---

            < VirtualHost >的標注部份改為

            NameVirtualHost *:443

            < VirtualHost *:443 >

 

五、修改httpd.conf

        1. 打開 C:\AppServ\Apache2.2\conf\httpd.conf檔案,修改部份如下:

            LoadModule ssl_module modules/mod_ssl.so #註解拿掉

            Include conf/extra/httpd-ssl.conf #註解拿掉

 

六、重新啟動Apache

      如果啟動過程中沒有出現錯誤,可以連 https://localhost 測試看看,應該就會出現要求憑證的認證畫面了。

        *如果Apache啟動失敗,請檢查 error.log或到 Windows 事件檢視器確認是哪邊設定錯誤。


楊 bey kuen 發表在 痞客邦 留言(0) 人氣()

24小時制日期顯示:
public class Datetime {

楊 bey kuen 發表在 痞客邦 留言(0) 人氣()

Timer與TimerTask應用。利用  timer.schedule(new RemindTask() , now, minutes * 2 * 1000),來實現從程式啟動後每隔幾秒鐘執行一次應用程式,並限定執行幾次後程式自動停止,範例如下:

package testjava;

import java.util.Date;
import java.util.Timer;

楊 bey kuen 發表在 痞客邦 留言(0) 人氣()

var hex_chr = "0123456789abcdef";
function rhex(num)

楊 bey kuen 發表在 痞客邦 留言(0) 人氣()

  在一個應用系統中常要定時去執行某些程式, 例如: 同步異質平台的資料, 或是定時發信件; Java是一種跨平台(Window/Linux)的程式語言, 雖然在 Window 平台可以用 AT 指令來作排程, 在 Linux 可以用 crontab 來排程, 但總是無法與系統作密切的結合.
quartz是個不錯的選擇, 它一樣可以跨異質平台作業, 而且與 Java 的結合也很密切, 只要在Java程式段有

public void execute(JobExecutionContext context)

楊 bey kuen 發表在 痞客邦 留言(0) 人氣()

參考程式碼如下:

import java.io.*;

楊 bey kuen 發表在 痞客邦 留言(0) 人氣()

Java Opensources for Web Development Part I:嘗試使用來自 Opensource 的小工具(1)
Lession 5 : JasperReport

 JasperSoft.com 原是一個 Report Engine 的供應商,但是將 ReportEngine 開放成為一個 SourceForge 上的 Opensource 之後,廣受 Java Community 的人們喜愛,非常便利地使用 iReport 就可以完成 Report Designing 的工作。而現在,JasperSoft 將這些 Opensource 聚合起來放到 JasperForge, 將挑戰 Java Opensource solution for BI 的動作,也將 JasperServer 逐步開放出來,一則借用 Opensource 的力量協助開發與除錯,另一方面更可以利用廣大的族群,來進行挑戰 CrystalReport 強大的商業市場競爭。  ◎ Overview JasperReport 本身是一個 ReportEngine,基本來說,我們可以製作 Report Template ( *.jrxml ),使用一些開發 jrxml 的工具如下

* iReport http://jasperforge.org/sf/projects/ireport

楊 bey kuen 發表在 痞客邦 留言(0) 人氣()

HowTo configure the C3P0 connection pool

Configuration

Here is a sample of C3P0 configuration. This is an extract of hibernate.cfg.xml:

<!-- configuration pool via c3p0--> 
<property name="c3p0.acquire_increment">1</property> 
<property name="c3p0.idle_test_period">100</property> <!-- seconds --> 
<property name="c3p0.max_size">100</property> 
<property name="c3p0.max_statements">0</property> 
<property name="c3p0.min_size">10</property> 
<property name="c3p0.timeout">100</property> <!-- seconds --> 
<!-- DEPRECATED very expensive property name="c3p0.validate>--> 

You also can set extra c3p0 properties using c3p0.properties. Put this file in the classpath (WEB-INF/classes for example), but be careful, the previous values will be overridden by Hibernate whether set or not (see below for more details). For more information on C3P0 configuration, please have a look at http://sourceforge.net/projects/c3p0 and unzip the download, there is a doc folder where you'll find everything you need.

For more on hibernate/c3p0 issues, see c3p0's docs at http://www.mchange.com/projects/c3p0/index.html#appendix_d

Parameters

c3p0 is very tunable, and offers a somewhat bewildering array of configuration parameters. For the complete list, see http://www.mchange.com/projects/c3p0/index.html#appendix_a

Here are a subset of the most important features:

initialPoolSize C3P0 default: 3

minPoolSize Must be set in hibernate.cfg.xml (or hibernate.properties), Hibernate default: 1

maxPoolSize Must be set in hibernate.cfg.xml (or hibernate.properties), Hibernate default: 100

idleTestPeriod Must be set in hibernate.cfg.xml (or hibernate.properties), Hibernate default: 0

If this is a number greater than 0, c3p0 will test all idle, pooled but unchecked-out connections, every this number of seconds.

timeout Must be set in hibernate.cfg.xml (or hibernate.properties), Hibernate default: 0

The seconds a Connection can remain pooled but unused before being discarded. Zero means idle connections never expire.

maxStatements Must be set in hibernate.cfg.xml (or hibernate.properties), Hibernate default: 0

The size of c3p0's PreparedStatement cache. Zero means statement caching is turned off.

propertyCycle Must be set in c3p0.properties, C3P0 default: 300

Maximum time in seconds before user configuration constraints are enforced. c3p0 enforces configuration constraints continually, and ignores this parameter. It is included for JDBC3 completeness.

acquireIncrement Must be set in hibernate.cfg.xml (or hibernate.properties), Hibernate default: 1

Determines how many connections at a time c3p0 will try to acquire when the pool is exhausted.

testConnectionOnCheckout Must be set in c3p0.properties, C3P0 default: false

Don't use it, this feature is very expensive. If set to true, an operation will be performed at every connection checkout to verify that the connection is valid. A better choice is to verify connections periodically using c3p0.idleConnectionTestPeriod.

autoCommitOnClose Must be set in c3p0.properties, C3P0 default: false

The JDBC spec is unfortunately silent on what should happen to unresolved, pending transactions on Connection close. C3P0's default policy is to rollback any uncommitted, pending work. (I think this is absolutely, undeniably the right policy, but there is no consensus among JDBC driver vendors.) Setting autoCommitOnClose to true causes uncommitted pending work to be committed, rather than rolled back on Connection close. [Note: Since the spec is absurdly unclear on this question, application authors who wish to avoid bugs and inconsistent behavior should ensure that all transactions are explicitly either committed or rolled-back before close is called.]

forceIgnoreUnresolvedTransactions Must be set in c3p0.properties, C3P0 default: false

Strongly disrecommended. Setting this to true may lead to subtle and bizarre bugs. This is a terrible setting, leave it alone unless absolutely necessary. It is here to work around broken databases / JDBC drivers that do not properly support transactions, but that allow Connections' autoCommit flags to be set to false regardless. If you are using a database that supports transactions "partially" (this is oxymoronic, as the whole point of transactions is to perform operations reliably and completely, but nevertheless, such databases exist), if you feel comfortable ignoring the fact that Connections with autoCommit == false may be in the middle of transactions and may hold locks and other resources, you may turn off c3p0's wise default behavior, which is to protect itself, as well as the usability and consistency of the database, by either rolling back (default) or committing (see c3p0.autoCommitOnClose above) unresolved transactions. This should only be set to true when you are sure you are using a database that allows Connections' autoCommit flag to go to false, but that it offers no other meaningful support of transactions. Otherwise setting this to true is just a bad idea.

numHelperThreads Must be set in c3p0.properties, C3P0 default: 3

c3p0 is very asynchronous. Slow JDBC operations are generally performed by helper threads that don't hold contended locks. Spreading these operations over multiple threads can significantly improve performance by allowing multiple operations to be performed simultaneously.

factoryClassLocation Must be set in c3p0.properties, C3P0 default: null

DataSources that will be bound by JNDI and use that API's Referenceable interface to store themselves may specify a URL from which the class capable of dereferencing a them may be loaded. If (as is usually the case) the c3p0 libraries will be locally available to the JNDI service, leave this set to null.  

以上參考網頁:http://www.hibernate.org/214.html

系統golive時這部份參數設置不當,結果導致DB Connection暴衝,一下子就給你從10、20給你一路衝到70以上;然後...再隔一陣子,DB就隔屁了...囧rz

楊 bey kuen 發表在 痞客邦 留言(0) 人氣()