Click here to Skip to main content
15,905,867 members
Home / Discussions / Java
   

Java

 
GeneralRe: How to write a program in java which stores every thing i type anywhere in a file Pin
TorstenH.23-Jan-12 19:34
TorstenH.23-Jan-12 19:34 
AnswerRe: How to write a program in java which stores every thing i type anywhere in a file Pin
Nagy Vilmos18-Jan-12 3:05
professionalNagy Vilmos18-Jan-12 3:05 
GeneralRe: How to write a program in java which stores every thing i type anywhere in a file Pin
TorstenH.18-Jan-12 3:22
TorstenH.18-Jan-12 3:22 
GeneralRe: How to write a program in java which stores every thing i type anywhere in a file Pin
anilgoyal128919-Jan-12 19:50
anilgoyal128919-Jan-12 19:50 
Questionremain application in the running when i exit it Pin
ZAliPour13-Jan-12 1:15
ZAliPour13-Jan-12 1:15 
AnswerRe: remain application in the running when i exit it Pin
TorstenH.13-Jan-12 2:07
TorstenH.13-Jan-12 2:07 
Questionmaintain connection pool issue Pin
c.thimmaiah12-Jan-12 23:55
c.thimmaiah12-Jan-12 23:55 
AnswerRe: maintain connection pool issue Pin
Nagy Vilmos13-Jan-12 0:38
professionalNagy Vilmos13-Jan-12 0:38 
Connections to what?

A common way to do this is to have a wrapper object that shares the connections. if you only need to hold the connection for a single method call it is easy, but if you need to maintain the use of a specific thread for several calls, then you need the concept of a lock, but here is a simple example of a connection pool:

Java
class WrapperClass (
    private static PoolClass pool = new PoolClass();
    private int calls;
    private ConnectionClass connection;

    public WrapperClass() {
        this.calls  = 0;
    }

    // every call to getConnection must be matched with a call to releaseConnection
    private ConnectionClass getConnection() {
        if (this.calls == 0) {
            this.connection =  WrapperClass.pool.getConnection(); 
        }
        this.calls++;
        return this.connection;
    }

    private void releaseConnection() {
        this.calls--;
        if (this.calls <= 0) {
            WrapperClass.pool.releaseConnection(this.connection);
            this.connection = null;
            this.calls  = 0;
        }
    }

    public void startMultipleUse() {
        this.getConnection();
    }
    public void useConnection() {
        ConnectionClass c = this.getConnection();

        // use c here and not the member variable

        this.releaseConnection();
    }
    public void endMultipleUse() {
        this.releaseConnection();
    }
)

Not that externally, the real connection - ConnectionClass - can never be accessed.
The PoolClass just instantiates the list of connections and hands them out on request and puts them back in the list when returned.


Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

AnswerRe: maintain connection pool issue Pin
jschell13-Jan-12 9:20
jschell13-Jan-12 9:20 
QuestionThreads for Beginners Pin
DRAYKKO77712-Jan-12 10:26
DRAYKKO77712-Jan-12 10:26 
AnswerRe: Threads for Beginners Pin
Luc Pattyn12-Jan-12 11:21
sitebuilderLuc Pattyn12-Jan-12 11:21 
QuestionDo event when variable is modified Pin
antonio34311-Jan-12 12:14
antonio34311-Jan-12 12:14 
AnswerRe: Do event when variable is modified Pin
TorstenH.11-Jan-12 21:29
TorstenH.11-Jan-12 21:29 
GeneralRe: Do event when variable is modified Pin
antonio34311-Jan-12 22:42
antonio34311-Jan-12 22:42 
GeneralRe: Do event when variable is modified Pin
Nagy Vilmos11-Jan-12 23:09
professionalNagy Vilmos11-Jan-12 23:09 
GeneralRe: Do event when variable is modified Pin
antonio34311-Jan-12 23:33
antonio34311-Jan-12 23:33 
GeneralRe: Do event when variable is modified Pin
TorstenH.12-Jan-12 2:26
TorstenH.12-Jan-12 2:26 
GeneralRe: Do event when variable is modified Pin
antonio34312-Jan-12 12:58
antonio34312-Jan-12 12:58 
GeneralRe: Do event when variable is modified Pin
TorstenH.12-Jan-12 21:34
TorstenH.12-Jan-12 21:34 
Questionredirecting and piping Pin
shekarchee11-Jan-12 11:31
shekarchee11-Jan-12 11:31 
AnswerRe: redirecting and piping Pin
Richard MacCutchan11-Jan-12 11:50
mveRichard MacCutchan11-Jan-12 11:50 
GeneralRe: redirecting and piping Pin
shekarchee11-Jan-12 13:59
shekarchee11-Jan-12 13:59 
GeneralRe: redirecting and piping Pin
Richard MacCutchan11-Jan-12 22:14
mveRichard MacCutchan11-Jan-12 22:14 
QuestionCalling java method from C# .net Pin
KASR111-Jan-12 0:26
KASR111-Jan-12 0:26 
AnswerRe: Calling java method from C# .net Pin
Nagy Vilmos11-Jan-12 0:42
professionalNagy Vilmos11-Jan-12 0:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.