Synchronized ConnectionPool access
Is there a way to avoid ResourceExceptions when performing
more requests for database connections from the pool than
are currently available?
Is there a kind of synchronized access which forces callers
to wait until used connections are released?
Regards
Osman
Larry Presswood wrote:
The issue is that at least in 6.0 sp2 rp2 is that this resource exception is thrown
even when there are enough DBMS connections.
We have a situation where we have 75 connections set up and max
in use at the time of the resource exception is around 7-10??
There appears to be a bug in 6.0??Well, yes, as you describe it, it sounds like a bug, but there are many current and
past customers of 6.0, and connection pools are probably the single most
travelled code path in the J2EE APIs, so we might have heard of this sooner.
An optimally crafted application can get away with one pool connection per execute
thread. How many execute threads do you have, and on what sort of machine are
you running? How many connections in the pool? Please describe how your code
obtains pool connections, and how it closes them.
thanks,
Joe
>
>
Joseph Weinstein wrote:
osman vurgun wrote:
Is there a way to avoid ResourceExceptions when performing
more requests for database connections from the pool than
are currently available?
Is there a kind of synchronized access which forces callers
to wait until used connections are released?
Regards
OsmanHi. This is a frequently asked question. Some day we'll put the answer in our
docs. No, there's no way to allow a caller to wait for a pool connection,
and from the system point of view there shouldn't be. Any caller waiting
is tieing up one of the fixed number of execute threads in the server, which
could otherwise be running another server task that has all it's resources
to do it's work. Enough 'patient waiters' could freeze the server solid,
especially if all the pool connections were being held by jobs on the server's
run queue, but would never run to release these connections because
all the execute threads were being held by waiters.
It is the system's responsibility to quickly reject any excess work, above the
amount it can handle, so it can continue to do work. Any system that permits
itself to devote it's resources to non-productive work is open to denial-of-
service attacks (inadvertant or intentional).
The fundamental problem is too few resources (pool conections) for the
work load, and the correct response is to make the pool bigger. Optimally
designed applications only require the server to have one pool connection
per execute thread.
Lastly, the proper application response to a resource exception is not
to do a tight loop retrying, at least not in serverside code. That accomplishes
the same tieing up of an execute thread. The exception should go back to
the external client for a retry from there.
Joe
Similar Messages
-
In a servlet i have a method which accesses the db to calculate the current max value of a numeric id field (which is the primary key of the table). Then the same method tries to insert a new record with id = max + 1.
If two users have that method executed "at the same time", it happens that the primary key is violated by one of the two, because after the 1st user (session) has his max calculated and before his max+1 id is inserted into the table, the 2nd user calculates his max as well, and of course it comes to be equal to the max of the 1st user.
I'm having an hard time in understanding synchronization; i syncronized the code block to the instance of the servlet (synchronized (this) { but the violations of the PK still occur.
Should i synchronize on the ServletContext ((synchronized (this.getServletContext()) {)?In the servlet where i must do the insertion of the new record with a new id, the servlet must then retrieve the new id just after the insertion, because the servlet itself needs the id of the new record for further processing.
It's true that i could make the db calculate the new id (which is also the primary key), using an autoincrementing data type for the field, or even just an INSERT SELECT MAX(ID) + 1 kind of statement, but if i do so, how do i then make the servlet aware of what the new id is ? That's the PK, so i wouldn't know how to SELECT it back after its creation (except by building some mechanism aimed solely at that and involving the creation of a new field, which i don't think it's how this quite generic problem is usually approached). -
ABAP Proxy synchronous interface - access to payload
Hi,
I have a scenario from ABAP Proxy -> XI -> Web Service.
On occassion, there is an issue with calling the Web Service (unavailable for instance) and an exception is returned to the ABAP Proxy for a SystemError.
I am able to catch this exception in the proxy and output the error - for example HTTP.Exception.HTTP.Adapter.
However, I also want to use information in the response payload to output on the error as the payload contain infrmation on why the error was thrown in the target application - for example - Format of data xxx is incorrect.
I can get access to the request payload using interface/class IF_WSPROTOCOL_PAYLOAD=>GET_SENT_REQUEST_PAYLOAD but there does not seem to be an equivalent for GET_RECEIVED_RESPONSE_PAYLOAD !
Have anyone done this before - use payload information in an ABAP proxy from the response payload ?
Kind regards
Colin.Hi,
I will need the actual payload info not the status.
I will have to debug SXI_MONITOR in CRM to see how it is obtained.
Thanks
Colin -
Performing a READ after WRITE corrupts the WRITE
I consider this a serious bug wich should be fixed as soon as possible.
Details can be found in the bugbase and at stackoverflow.
I would like to ask here to vote the bug in the bugbase, so adobe might consider fixing it.
tx leoThank you for posting and reporting the bug. I'd like to echo your request that others effected take a minute and leave a comment/vote for this issue. I'll also ask the team to investigate.
Thanks,
Chris -
Imlementing synchronized access to shared objects in LabVIEW OOP
Many objects in object-oriented programming have an
identity, such as a file, a front-panel object or a hardware device.
These objects cannot be modelled using present LabVOOP (LabVIEW Object Oriented Programming) objects as
LabVOOP objects gets copied as wire is branched; multiple different
wires cannot all represent a single object. This issue has been
irritating the community of LabVIEW users since the release of LabVOOP
a few months ago.
It seems that there is a huge demand for
objects with unique identity i.e. by-reference objects in LabVIEW. The
central problem why LabVOOP propably doen't have these objects is the
difficulty in implementing synchronized access to these objects from
multiple parallel threads. The problem of synchronized access can be
divided into two different separate topics. First how the
sychronization should be implemented in LabVIEW runtime engine. Second
how this synchronization mechanism should be visible to the developer.
I'd like to start this thread to discuss these two issues.
Synhronization under the hood
Traditionally
people talk about locking of an object and about get-modify-set pass
when accessing the object. Locking is traditionally done by acquiring a
mutex for an object, modifying the object and releasing the mutex so
that other threads can access the same object instance. This is how
inter-thread synchronization is traditionally done. However, besides
the mutex based locking, the computer science community has innovated
also different kinds of methods on synchronizing the access to objects.
One way to get object-level synchronization is modify the
runtime engine so that it only allows a single method of a synchronized
object to run at any time. This mechanism of syncrhonization is
implemented in programming languages like O'Haskell, which is a Haskell
variant with object orirented features.
Also different
transactional mechanisms[1,2] have been successful. In transactional
mechanisms multiple threads are allowed to access a synchronized object
simultaneously. As each method accessing an object commits their
changes, they verify that no other object has modified the object
simultaneously in a manner than would break the transaction. If such a
modification has occurred, everything is rolled back. Transactional
mechanism do not suit to every possible situation as not everything can
be rolled back. For example it's hard to roll back an action that
somehow modifies the physical world.
User experience of synchronization
How
the synchronization is generally implemented in LabVIEW shouldn't be
directly visible to the developer end-user. The developer should
understand the general concepts of synchronization to take full
advantage of it, but in general the synhronization mechanism should be
integrated directly to development environment. There should in general
be no need to acquire a mutex by calling acquire mutex node but instead
the end-user should be able to specify which data needs synhronized
access in more sophisticated way.
In the following I propose a
mechanism of integrating the synchronized access of by-ref objects to
the development environemnt of LabVIEW. The proposal is very
preliminary but I hope it breaks the ice and the community would start
innovating in how should NI implement the syncrhonization support in
the user interface of LabVIEW.
Wire level synchronization
Only
methods can access object private data members. In synchronized access
to the object, it's the methods accessing the private data members that
need to be synchronized. The private data members are accessed by
applying unbundle node to the class wire and data is written back to
the object using bundle node.
What I propose is the following.
An unbundle node could either be normal or "synchronized". A
synchronized unbundle would guarantee the access to the private data
members in synchronized manner. All data wires originating from
synchronized unbundle would be of synchronized type, in a little
similar manner as a dynamic dispatch wire is of special dynamic
dispatch type. Such a wire must evetually be connected to a bundle
node. When the wire is bundled back to the originating object, the
synchronization requirement is released.
These synchronized
wires would look somewhat different from normal wires so that the
developer instantly knows that the wire is synchronized. The developer
can branch the wire, but only one wire branch can own the synchronized
type. The developer could easily select which wire would be
syncrhonized by Ctrl+clicking the wire. Such a wire can be considered
as a combination of a data and a mutex, even though mutexes don't need
to be the underlying synchronization method. The wire just guarantees
that there is a mechanism in the runtime engine that makes sure the
access to the wire data is synchronized.
There is a need to wire
data originating from a non-synchronized wire to a synchronized wire so
that it can replace the private data member of the class. This is
accomplished with a new node similar to bundle node, that would allow
replacing the data in a syncrhonized wire with some data originating
from a non-synchronized wire.
The synchronized wire can be
connected to a front panel controls of special syncrhonized type. This
way the synchronized wire can originate from a method and allow passing
the synchronized data to the calling VI and back to another method.
This is practical for example in a situation when the developer wants
to run different analyzes to a data class but don't want to rewrite all
the existing data analysis tools as class members. So the developers
writes a syncrhonization acquiring getData method that let's the
calling VI to access the syncrhonized data. Then the developer passes
this data to an analysis VI and passes the result back to a setData
method that writes the result back to the class wire.
There
will probably be technical problems in allowing the user to connect
such a synchronized wire to all existing VIs since these VIs. Therefore
the programming model for all nodes that do not support such
synchronized wires will be branching the wire and passing the
non-synchronized wire branch to the node and then bundling the result
back to the synchronized wire.
To increase performance and
decrease unnecessary buffer copies when a syncrhonized wire is
branched, if the syncrhonized wire continues directly to the new bundle
synchronized wire node, no buffer copy is made.
Discussion
The
syncrhonized access to a by-ref LabVOOP objects can be implemented in
multiple ways by National Instruments. The synchronized access should
be divided to two different and independent parts: 1) the user
experience of synchronization and 2) the runtime engine synchronization
mechanisms. As LabVOOP objects have special properties compared to
other LabVIEW data types, optimal user experience can be gained by
designing the user experience specifically for LabVOOP objects. From
user experience point-of-view this syncrhonization mechanism may not
work for other data types. Separating object syncrhonization from
synchronization of other data types is advantageous also for other
reasons. Due to the fact that object data can only be accessed via
object methods, more advanced synchronization methods may be used with
objects than can be used with other data types. O'Haskell
synchronization implementation is an example of this. Integrating the
synchronization directly to the user interface allows NI to change the
mehcanisms under the hood, when computer science comes up with more
advanced methods. Therefore NI could begin with traditional and quite
easy mutex-based synchronization and later move to more advanced
perhaps transaction based syncrhonization methods or even combinations
of multiple different methods.
I hope this topic generates
discussion that would help NI to implement an excellent synchronization
mechanism in LabVOOP. I hope that all talented individuals in the
community participate this discussion to help NI to reach this goal. I
also hope that if you just have time, it would be great if you could
surf the computer science resources to find out what kinds of new
techniques there exists for synchronizing access to shared resources. A
Large community may find much more innovative solutions than a few engineers at NI. Let's give NI the power of open source design
Tomi MailaHello Tomi,
First, thank you for taking the time to write such a well
though-out suggestion. Are you familiar
with the “LabVIEW Object-Oriented Programming: The Decisions Behind the Design”
document? I think the reason we chose to implement a ‘by
value’ strategy, is that is more in line with the LabVIEW programming paradigm
of dataflow, and would make sense to most of our LabVIEW users.
I think your suggestion is interesting, and it does
highlight the need to think outside of the conventional LabVIEW box and look to
some of the innovative things other languages do. However, I think we all agree that
synchronization takes careful planning and extra work for the programmer. Even with an ‘ideal’ solution I see no way
around this. For LabVIEW users today,
one great way to get synchronized ‘by reference’ semantics with your objects is
to use a single-element queue to pass your object. The queue itself is passed ‘by reference’ and
is inherently synchronized! The does
have the disadvantage of adding one more small layer of complexity to your
program, but some complexity would have to be introduced in any situation. The other disadvantage with this is that it
is not always an intuitive way to implement your program and requires some
amount of LabVIEW knowledge before one would generally come across this
technique.
In any case, I appreciate the time and effort you put in to
your suggestion. Please make sure that
you submit the suggestion formally through the NI Product Suggestion Center so
that it can be reviewed by some of the decision makers here.
Thanks again,
Travis M
LabVIEW R&D
National Instruments -
PI SOAP access to third party Webservice,Return ERROR
Hi Experts,
I have one soap synchronous scenario access to third party WEBSERVICE,Return error.While testing the wsdl in soap ui, I am getting response, messages showing successfully processed. I think it is pi to send xmlns:ns1='http://tempuri.org/' on both sides should not be a single quotation mark, but I don't know how to adjust PI set. how do you see.
1、SOAP UI submitted to Webserver XML
[2014-05-26 08:47:24.662] --- Recv data from SocketId=272 Socket=10880
POST /MWGate/wmgw.asmx HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: text/xml;charset=UTF-8
SOAPAction: "http://tempuri.org/MongateCsSpSendSmsNew"
User-Agent: Jakarta Commons-HttpClient/3.1
Host: 10.0.0.253:8082
Content-Length: 520
<soapen:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapen:Header />
<soapen:Body>
<tem:MongateCsSpSendSmsNew>
<tem:userId>DOA001</tem:userId>
<tem:password>dennis</tem:password>
<tem:pszMobis>1313773654</tem:pszMobis>
<tem:pszMsg>1111</tem:pszMsg>
<tem:iMobiCount>1</tem:iMobiCount>
<tem:pszSubPort>*</tem:pszSubPort>
</tem:MongateCsSpSendSmsNew>
</soapen:Body>
</soapen:Envelope>
2、PI submitted to Webserver XML
[2014-05-26 08:36:08.725] --- Recv data from SocketId=271 Socket=10704
POST /MWGate/wmgw.asmx HTTP/1.0
Accept: */*
Host: 10.0.0.253:8082
User-Agent: SAP-Messaging-com.sap.aii.af.sdk.xi/1.0505
CallingType: SA
content-id: <[email protected]>
Content-Type: text/xml; charset=utf-8
Content-Length: 417
SOAPACTION: "http://tempuri.org/MongateCsSpSendSmsNew"
<SOAP:Envelope xmlns:SOAP='http://schemas.xmlsoap.org/soap/envelope/'>
<SOAP:Header />
<SOAP:Body>
<ns1:MongateCsSpSendSmsNew xmlns:ns1='http://tempuri.org/'>
<ns1:userId>DOA001</ns1:userId>
<ns1:password>dennis</ns1:password>
<ns1:pszMobis>13637731567</ns1:pszMobis>
<ns1:pszMsg>Constant</ns1:pszMsg>
<ns1:iMobiCount>1</ns1:iMobiCount>
<ns1:pszSubPort>*</ns1:pszSubPort>
</ns1:MongateCsSpSendSmsNew>
</SOAP:Body>
</SOAP:Envelope>
3、SXI_MONITOR Payloads content:
<ns1:MongateCsSpSendSmsNew xmlns:ns1="http://tempuri.org/">
<ns1:userId>DOA001</ns1:userId>
<ns1:password>dennis</ns1:password>
<ns1:pszMobis>13637731567</ns1:pszMobis>
<ns1:pszMsg>Constant</ns1:pszMsg>
<ns1:iMobiCount>1</ns1:iMobiCount>
<ns1:pszSubPort>*</ns1:pszSubPort>
</ns1:MongateCsSpSendSmsNew>
4、PI channelHi Nathan,
Have you tried the SOAP HTTP Axis function.
http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/b092777b-ee47-2a10-17b3-c5f59380957f?overridelayout=t…
https://help.sap.com/saphelp_nw04/helpdata/en/45/a39e244b030063e10000000a11466f/content.htm
Configuring the Receiver Axis SOAP Adapter (SAP Library - SAP Exchange Infrastructure)
Regards,
Jannus Botha -
How Do I Sync All Email Accounts and Mailboxes (including those I create)?
How do I Sync all email accounts in the OSX Mail Application and mailboxes(including those I have created), all using the .mac sync application . . . or is there a better way to sync information from my powerbook to my imac?
The problem: I am reading and sorting/organizing my emails on my powerbook when traveling, and then I come home to my imac and end up having to manually duplicate the work I just completed. Any easy way of truly syncing the information between computers? Pleae let me know. Thanks
What is meant by mailbox structures? For example: On my powerbook, I have 2 unread messages in one of my IMAP email accounts. I read both messages. I choose to move message A to a client mailbox that I created in the OSX Mail APPL and the other message B I leave as read in the inbox & respond with a sent email . Now I use the .mac sync app on the powerbook. Then I use the .mac sync app on my imac . . . . will the application not sync the same information/sorting/organizing/sent responces into the appropriate mailboxes? I'm so confused.
Also, what "mailboxes behavior" & "advanced" preferences should be checked with IMAP email accounts. All sorts of misc. mailboxes keep being created in the OSX Mail APP. when choosing different preferences. When selecting store on server (mailboxes created indicate: stored locally on computer). Again very confused.The benefit of using an IMAP is keeping server stored messages synchronized when accessing the account with an email client such as the Mail.app on different Macs.
Messages in the Inbox mailbox for an IMAP type account are stored on the server by default which cannot be turned off.
With an IMAP type account, you can also store Drafts, Sent, Trash and Junk messages on the server which is available under the Mailbox Behaviors tab for the account preferences.
If you want to keep Sent messages synchronized with Mail on both Macs, you need to select "Store sent messages on the server" under the Mailbox Behaviors tab for the account preferences in Mail on both Macs.
I choose to move message A to a client mailbox that I created
in the OSX Mail APPL and the other message B I leave as read in
the inbox & respond with a sent email . Now I use the .mac
sync app on the powerbook. Then I use the .mac sync app on
my imac . . . . will the application not sync the same information
/sorting/organizing/sent responces into the appropriate mailboxes?
I'm so confused.
Was this client mailbox created as an "On My Mac" location mailbox? If so, an "On My Mac" location mailbox is stored locally on the hard drive, not on the server so messages moved to such a mailbox will not be kept synchronized with Mail on both Macs. Transferring a message from the account's Inbox mailbox or from any server stored mailbox to an "On My Mac" location mailbox stores the transferred message locally on the hard drive and removes the message from the server at the same time.
When creating a mailbox, the default location is "On My Mac" which is stored locally on the hard drive. If you are accessing an IMAP type account, you can select the IMAP account via the location selection which will create the mailbox on the server and be available in Mail on the other Mac. Messages placed in a server stored mailbox will be kept synchronized with Mail on both Macs. -
Java synchronization mechanism allows two threads to hold the same lock?
Dear all,
I am a little bit puzzled since I cannot explain why I get the following behavior.
I have a small Java program to draw components. The drawings are cached in the memory for performance improvements. The cache is access by two different threads. Now the funny thing: even I synchronized every access to the cache, I still get ConcurrentModificationExceptions.
Here is a part of the code:
@Override public void paint(Graphics g2) {
// Check if the image is in the cache
Image drawing;
synchronized (m_Cache) { drawing = m_Cache.get(m_CurrentOffset); }
if (drawing == null) {
// The image is not cached, so draw it
// Put the image into the cache
synchronized (m_Cache) { m_Cache.put(m_CurrentOffset, drawing); }
public void componentResized(ComponentEvent e) {
// Upon resizing of the component, adjust several pre-calculated values
// And flush the cache
synchronized (m_Cache) { m_Cache.clear(); }
public void elementUpdated(IHexGridElement Element) {
// Clear cache where the element may be contained at
synchronized (m_Cache) { for (Point p : m_Cache.keySet()) { if (isElementInScope(Element.getIndex(), p, new Point(p.x + m_MaxColumn, p.y + m_MaxRow))) { m_Cache.remove(p); } } }
}The paint and componentResized methods are invoked by the AWTEventQueue-0 thread. The elementUpdated method is invoked by "MyThread" thread.
Now my question is why do I get java.util.ConcurrentModificationException, especially in situations where a lot of repaintings have to be done and the window is resized? When I remove the caching stuff, everything works perfect, but only a little bit slow.
Thanks for any help.In your elementUpdated method, you are using an Iterator to walk over the set of keys in your map. You can't see the Iterator, because you are using the new for-each loop syntax which hides it from you - but it's there under the covers. You are then removing elements from your map directly. This is what causes your ConcurrentModificationException.
You should re-write your loop to explicitly use an iterator, and then do your remove operation through the iterator.
Incidentally, you have a slight race condition in your paint method. Two (or more) threads could simultaneously discover that a given image is not cached, then both draw it, then both cache it. If the drawing operation produces the same drawing every time, then the only problem this causes is the overhead of drawing the image multiple times. To fix this, wrap the get, draw and put operations in a single synchronized block. -
Using database connection in a servlet and get errors after 8 hours
Hey,
I'm running a poker script using applet/servlets and it works great. But for some reason about about 8 hours that database layer stops working. At first I thought it was the connections to mySQL that were timing out (because im using connection pooling) but after turning pooling off (I now create the connection each time) I'm still seeing that same error (I can create a connection but when I do an action ex. like a select statment I get an error). What i'm wondering could it be that the driver I load with Class.forName() some how unloads it's self after x amount of time not being used? Not sure if that is it but if anyone could give me some insight that would be great. The Error i recieve is below:
INFO: Database Event:DatabaseController: Error executing database query.
ERROR: Communications link failure due to underlying exception:
** BEGIN NESTED EXCEPTION **
java.net.SocketException
MESSAGE: Software caused connection abort: recv failed
STACKTRACE:
java.net.SocketException: Software caused connection abort: recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at com.mysql.jdbc.util.ReadAheadInputStream.fill(ReadAheadInputStream.java:104)
at com.mysql.jdbc.util.ReadAheadInputStream.readFromUnderlyingStreamIfNecessary(ReadAheadInputStream.java:144)
at com.mysql.jdbc.util.ReadAheadInputStream.read(ReadAheadInputStream.java:172)
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1839)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2288)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2784)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1531)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1622)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2370)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2297)
at com.mysql.jdbc.Statement.executeQuery(Statement.java:1183)
at com.softnet.database.DatabaseController.executeDatabaseQuery(DatabaseController.java:190)
at com.softnet.games.GameServer.validateUser(GameServer.java:438)
at com.softnet.games.GameServer.handleData(GameServer.java:113)
at com.softnet.network.HttpConnectionThread.run(HttpServletListener.java:191)
** END NESTED EXCEPTION **
I know the query is good because it works all other times just not after about 8 hours.
--Z3r0CooLHey,
Thanks for the responces. For the connection pooling I would open 5 connections and keep them open. So i though maybe after 8 hours after not being used they would timeout. Thats why i turned off conection pooling and create a new connection each time. Anyways i'll post the code below incase i made a mistake somewhere.
package com.softnet.database;
/************************ DatabaseControler **************************/
import java.sql.*;
import java.util.*;
import com.softnet.database.DatabaseConnectionPool;
import com.softnet.database.DatabaseSettings;
public class DatabaseController
implements DatabaseListener
//Used to make sure the database driver is loaded
private boolean databaseDriverState = false;
//Used to store a database connection
private Connection databaseConnection = null;
//If to user connection pooling or not
private boolean useConnectionPooling = false;
//Used to hold the connection pool varible
private DatabaseConnectionPool connectionPool = null;
//Used to store database settings
private DatabaseSettings databaseSettings;
//Used to hold the DatabaseController listeners
private List databaseControllerListeners = new ArrayList();
//min number of connections for connection pool
private int minNumberOfConnections = 1;
//max number of connections for connection pool -1 is unlimited
private int maxNumberOfConnections = -1;
//DatabaseController Constructors
public DatabaseController(DatabaseSettings databaseSettings)
this.databaseSettings = databaseSettings;
databaseDriverState = loadDatabaseDriver(databaseSettings.getDatabaseDriver());
public DatabaseController(DatabaseSettings databaseSettings, boolean useConnectionPooling, int minNumberOfConnections, int maxNumberOfConnections)
this.databaseSettings = databaseSettings;
this.useConnectionPooling = useConnectionPooling;
this.minNumberOfConnections = minNumberOfConnections;
this.maxNumberOfConnections = maxNumberOfConnections;
if(useConnectionPooling == true)
connectionPool = new DatabaseConnectionPool(databaseSettings, minNumberOfConnections, maxNumberOfConnections);
connectionPool.addDatabaseListener(this);
else
databaseDriverState = loadDatabaseDriver(databaseSettings.getDatabaseDriver());
public DatabaseController() {}
//Database Settings Get/Set
public DatabaseSettings getDatabaseSettings()
return databaseSettings;
public void setDatabaseSettings(DatabaseSettings databaseSettings)
this.databaseSettings = databaseSettings;
//Connection Pooling Get/Set
public boolean getConnectionPooling()
return useConnectionPooling;
public void setConnectionPooling(boolean useConnectionPooling, int minNumberOfConnections, int maxNumberOfConnections)
this.useConnectionPooling = useConnectionPooling;
this.minNumberOfConnections = minNumberOfConnections;
this.maxNumberOfConnections = maxNumberOfConnections;
if(useConnectionPooling == true)
if(connectionPool == null)
connectionPool = new DatabaseConnectionPool(databaseSettings, minNumberOfConnections, maxNumberOfConnections);
connectionPool.addDatabaseListener(this);
else
if(connectionPool != null)
connectionPool.destroyConnections();
connectionPool.removeDatabaseListener(this);
connectionPool = null;
//Return if there connected
public boolean isConnected()
boolean isConnected;
if(databaseConnection != null)
isConnected = true;
else
isConnected = false;
return isConnected;
//Used to connect to database or get a connection for the connection pool
public void connect()
if(databaseDriverState == false)
databaseDriverState = loadDatabaseDriver(databaseSettings.getDatabaseDriver());
//If we dont have a current connection, make one
if(databaseConnection == null && databaseDriverState == true)
if(useConnectionPooling == false)
try
databaseConnection = DriverManager.getConnection(databaseSettings.getDatabaseURL(), databaseSettings.getUserName(), databaseSettings.getUserPassword());
catch (SQLException sqle)
//Raise event
raiseDatabaseEvent("DatabaseController: Error connecting to database. \nERROR: " + sqle.getMessage());
databaseConnection = null;
else
databaseConnection = connectionPool.getConnection();
//Used to disconnect from the database or give back the connection to the connection pool
public void disconnect()
if(databaseConnection != null)
if(useConnectionPooling == false)
try
//Close DB Connection
databaseConnection.close();
catch(SQLException ignore) {}
finally
databaseConnection = null;
else
connectionPool.returnConnection(databaseConnection);
databaseConnection = null;
public ResultSet executeDatabaseQuery(String sSQL)
ResultSet databaseResult = null;
if(databaseConnection != null)
try
Statement databaseStatement = databaseConnection.createStatement();
databaseResult = databaseStatement.executeQuery(sSQL);
catch(SQLException sqle)
//Raise event
raiseDatabaseEvent("DatabaseController: Error executing database query.\nSQL: " + sSQL + "\nERROR: " + sqle.getMessage());
return databaseResult;
public int executeDatabaseUpdate(String sSQL)
int rowsAffected = -1;
if(databaseConnection != null)
try
Statement databaseStatement = databaseConnection.createStatement();
rowsAffected = databaseStatement.executeUpdate(sSQL);
catch(SQLException sqle)
//Raise event
raiseDatabaseEvent("DatabaseController: Error executing database update.\nSQL: " + sSQL + "\nERROR: " + sqle.getMessage());
return rowsAffected;
//Used to load the Database Driver
private boolean loadDatabaseDriver(String databaseDriver)
boolean driverLoaded;
if(databaseDriver.equals("") == false)
try
//Load Database Driver
Class.forName(databaseDriver).newInstance();
driverLoaded = true;
catch (Exception e)
//Raise event
raiseDatabaseEvent("DatabaseController: Error loading database driver. \nERROR: " + e.getMessage());
driverLoaded = false;
else
driverLoaded = false;
return driverLoaded;
//Wrap the DatabaseConnectionPool Error to the DatabaseController
public void databaseEventOccurred(DatabaseEvent de)
raiseDatabaseEvent(de.getErrorMessage());
//Event Handling Code
//Used to add database listeners (Its sync'd so you can change the listeners when firing an event)
public synchronized void addDatabaseListener(DatabaseListener databaseControllerListener)
databaseControllerListeners.add(databaseControllerListener);
//Used to remove a listener from the list (Its sync'd so you can change the listeners when firing an event)
public synchronized void removeDatabaseListener(DatabaseListener databaseControllerListener)
databaseControllerListeners.remove(databaseControllerListener);
//Used to send the raise event to the listeners
private synchronized void raiseDatabaseEvent(String databaseError)
DatabaseEvent databaseEvent = new DatabaseEvent(this, databaseError);
Iterator listeners = databaseControllerListeners.iterator();
while(listeners.hasNext())
DatabaseListener listener = (DatabaseListener) listeners.next();
listener.databaseEventOccurred(databaseEvent);
/********************* DatabaseConnectionPool **************/
package com.softnet.database;
import java.io.*;
import java.sql.*;
import java.util.*;
import com.softnet.database.*;
import com.softnet.database.DatabaseSettings;
public class DatabaseConnectionPool
//min number of connections
private int minNumberOfConnections = 1;
//max number of connections -1 is unlimited
private int maxNumberOfConnections = -1;
//Store the connections
protected Hashtable databaseConnections = null;
//Database Info
protected DatabaseSettings databaseSettings;
//to hold Driver state
private boolean databaseDriverState = false;
//To hold connection checker
private DatabaseConnectionCheck connectionChecker = null;
//Used to hold the DatabaseConnectionPool listeners
private List databaseConnectionPoolListeners = new ArrayList();
public DatabaseConnectionPool(DatabaseSettings databaseSettings, int minNumberOfConnections, int maxNumberOfConnections)
this.databaseSettings = databaseSettings;
this.minNumberOfConnections = minNumberOfConnections;
this.maxNumberOfConnections = maxNumberOfConnections;
//Load Driver
databaseDriverState = loadDatabaseDriver(databaseSettings.getDatabaseDriver());
//Create connection
createConnections();
public DatabaseConnectionPool(int minNumberOfConnections, int maxNumberOfConnections)
this.minNumberOfConnections = minNumberOfConnections;
this.maxNumberOfConnections = maxNumberOfConnections;
//Database Settings Get/Set
public DatabaseSettings getDatabaseSettings()
return databaseSettings;
public void setDatabaseSettings(DatabaseSettings databaseSettings)
this.databaseSettings = databaseSettings;
//Driver State Get
public boolean getDatabaseDriverState()
return databaseDriverState;
public void createConnections()
if(databaseDriverState == false)
databaseDriverState = loadDatabaseDriver(databaseSettings.getDatabaseDriver());
//Create all connections and load the minimum in the Hashtable
if(databaseConnections == null)
if(databaseDriverState == true && minNumberOfConnections != 0)
databaseConnections = new Hashtable();
for(int i = 0; i < minNumberOfConnections; i++)
try
databaseConnections.put(DriverManager.getConnection(databaseSettings.getDatabaseURL(), databaseSettings.getUserName(), databaseSettings.getUserPassword()), Boolean.FALSE);
catch(SQLException sqle)
//Problem break loop and destroy any connections
destroyConnections();
//Raise event
raiseDatabaseEvent("DatabaseConnectionPool: Error creating database connections. \nERROR: " + sqle.getMessage());
break;
//If no connection check exists create one
if(connectionChecker == null)
connectionChecker = new DatabaseConnectionCheck(this);
connectionChecker.start();
public Connection getConnection()
Connection connection = null;
boolean errorWithConnection = false;
Enumeration connections = databaseConnections.keys();
synchronized (databaseConnections)
while(connections.hasMoreElements())
errorWithConnection = false;
connection = (Connection) connections.nextElement();
Boolean state = (Boolean) databaseConnections.get(connection);
//If connection is not used, use it.
if(state == Boolean.FALSE)
try
connection.setAutoCommit(true);
catch(SQLException e)
//Problem with connection remove connection and replace it
databaseConnections.remove(connection);
try
connection = DriverManager.getConnection(databaseSettings.getDatabaseURL(), databaseSettings.getUserName(), databaseSettings.getUserPassword());
catch(SQLException sqle)
errorWithConnection = true;
if(errorWithConnection == false)
// Update the Hashtable to show this one's taken
databaseConnections.put(connection, Boolean.TRUE);
// Return the connection
return connection;
//All connections being used check to max to see if we can make a new one
if(maxNumberOfConnections == -1 || maxNumberOfConnections > databaseConnections.size())
try
connection = DriverManager.getConnection(databaseSettings.getDatabaseURL(), databaseSettings.getUserName(), databaseSettings.getUserPassword());
catch(SQLException sqle)
errorWithConnection = true;
if(errorWithConnection == false)
databaseConnections.put(connection, Boolean.TRUE);
return connection;
//If not connections free and max connections reached wait for a free connection
return getConnection();
public void returnConnection(Connection connection)
boolean errorWithConnection = false;
//Make sure connection still works
try
connection.setAutoCommit(true);
catch(SQLException e)
//Problem with connection remove connection and replace it
databaseConnections.remove(connection);
try
connection = DriverManager.getConnection(databaseSettings.getDatabaseURL(), databaseSettings.getUserName(), databaseSettings.getUserPassword());
catch(SQLException sqle)
errorWithConnection = true;
if(errorWithConnection == false)
databaseConnections.put(connection, Boolean.FALSE);
public void destroyConnections()
Connection connection = null;
if(databaseConnections != null)
//Close all connections
Enumeration connections = databaseConnections.keys();
while (connections.hasMoreElements())
connection = (Connection) connections.nextElement();
try
connection.close();
catch(SQLException ignore) {}
//Free up hashtable
databaseConnections = null;
private boolean loadDatabaseDriver(String databaseDriver)
boolean driverLoaded;
if(databaseDriver.equals("") == false)
try
//Load Database Driver
Class.forName(databaseDriver);
driverLoaded = true;
catch (ClassNotFoundException cnfe)
//Raise event
raiseDatabaseEvent("DatabaseController: Error loading database driver. \nERROR: " + cnfe.getMessage());
driverLoaded = false;
else
driverLoaded = false;
return driverLoaded;
//Event Handling Code
//Used to add database listeners (Its sync'd so you can change the listeners when firing an event)
public synchronized void addDatabaseListener(DatabaseListener databaseConnectionPoolListener)
databaseConnectionPoolListeners.add(databaseConnectionPoolListener);
//Used to remove a listener from the list (Its sync'd so you can change the listeners when firing an event)
public synchronized void removeDatabaseListener(DatabaseListener databaseConnectionPoolListener)
databaseConnectionPoolListeners.remove(databaseConnectionPoolListener);
//Used to send the raise event to the listeners
private synchronized void raiseDatabaseEvent(String databaseError)
DatabaseEvent databaseEvent = new DatabaseEvent(this, databaseError);
Iterator listeners = databaseConnectionPoolListeners.iterator();
while(listeners.hasNext())
DatabaseListener listener = (DatabaseListener) listeners.next();
listener.databaseEventOccurred(databaseEvent);
class DatabaseConnectionCheck extends Thread
private DatabaseConnectionPool connectionPool;
DatabaseConnectionCheck(DatabaseConnectionPool connectionPool)
this.connectionPool = connectionPool;
public void run()
try
while(true)
//check threads every 30 seconds
this.sleep(300000);
if(connectionPool.databaseConnections != null)
Connection connection = null;
Enumeration connections = connectionPool.databaseConnections.keys();
synchronized (connectionPool.databaseConnections)
while(connections.hasMoreElements())
connection = (Connection) connections.nextElement();
Boolean state = (Boolean) connectionPool.databaseConnections.get(connection);
//If connection is not used, use it.
if(state == Boolean.FALSE)
try
connection.setAutoCommit(true);
catch(SQLException e)
//Problem with connection remove connection and replace it
connectionPool.databaseConnections.remove(connection);
try
connection = DriverManager.getConnection(connectionPool.databaseSettings.getDatabaseURL(), connectionPool.databaseSettings.getUserName(), connectionPool.databaseSettings.getUserPassword());
catch(SQLException sqle)
connection = null;
// Update the Hashtable with new connection if its not null
if(connection != null)
connectionPool.databaseConnections.put(connection, Boolean.FALSE);
catch(InterruptedException ignored) {}
Basicly the why it works is the connection pool hold the database connections. When the user needs a connection they use the database controller to request a connection (By create a instance and called the connect() method) and the connection is either created or grabed from the connection pool. After the user is done with the connection they call the disconnect() method which closes the connection or returns it to the connection pool.
--Z3r0CooL -
Using WLST to clone basic Portal domain
As a way to validate the use of WLST for our environment, I am trying to use configToScript and execfile to duplicate a basic Portal domain created using the Configuration Wizard (with no subsequent modifications). The configToScript command seems to execute with no problems but when I try to re-create the domain using exec file, I am getting the exception shown below. This happens for each Connection Pool in the configuration.
Any suggestions would be appreciated...
Environment:
OS - WinXP SP2
Java - 1.5.0_07
WLST - wlst64 (downloaded from dev2dev on 6/23)
WLP - 8.1.4
<pre>
JDBCConnectionPool with name 'cgPool' has been created successfully.
<Jun 27, 2006 3:51:31 PM EDT> <Error> <Management> <BEA-140001> <An error occurred while getting attribute Password on MBean wk7804_portalDomain:Location=portalServer,Name=cgPool,Type=JDBCConnectionPoolConfig. Method: null. Exception: com.rsa.jsafe.JSAFE_PaddingException: Could not perform unpadding: invalid pad byte..
com.rsa.jsafe.JSAFE_PaddingException: Could not perform unpadding: invalid pad byte.
at com.rsa.jsafe.JA_PKCS5Padding.performUnpadding([BIILjava.lang.Object;)I(Unknown Source)
at com.rsa.jsafe.JG_BlockCipher.decryptFinal([BI)I(Unknown Source)
at weblogic.security.internal.encryption.JSafeEncryptionServiceImpl.decryptBytes([B)[B(JSafeEncryptionServiceImpl.java:67)
at weblogic.security.internal.encryption.JSafeEncryptionServiceImpl.decryptString([B)Ljava.lang.String;(JSafeEncryptionServiceImpl.java:93)
at weblogic.security.internal.encryption.ClearOrEncryptedService.decrypt(Ljava.lang.String;)Ljava.lang.String;(ClearOrEncryptedService.java:56)
at weblogic.management.EncryptionHelper.decryptString([B)Ljava.lang.String;(EncryptionHelper.java:45)
at weblogic.management.internal.DynamicMBeanImpl.getAttribute(Ljava.lang.String;)Ljava.lang.Object;(DynamicMBeanImpl.java:624)
at weblogic.management.internal.ConfigurationMBeanImpl.getAttribute(Ljava.lang.String;)Ljava.lang.Object;(ConfigurationMBeanImpl.java:179)
at com.sun.management.jmx.MBeanServerImpl.getAttribute(Ljava.lang.Object;Ljava.lang.String;)Ljava.lang.Object;(MBeanServerImpl.java:1186)
at com.sun.management.jmx.MBeanServerImpl.getAttribute(Ljavax.management.ObjectName;Ljava.lang.String;)Ljava.lang.Object;(MBeanServerImpl.java:1156)
at weblogic.management.internal.RemoteMBeanServerImpl.getAttribute(Ljavax.management.ObjectName;Ljava.lang.String;)Ljava.lang.Object;(RemoteMBeanServerImpl.java:288)
at weblogic.management.internal.MBeanProxy.getAttribute(Ljava.lang.String;)Ljava.lang.Object;(MBeanProxy.java:610)
at weblogic.management.internal.MBeanProxy.invokeForCachingStub(Ljava.lang.String;[Ljava.lang.Object;)Ljava.lang.Object;(MBeanProxy.java:442)
at weblogic.management.configuration.JDBCConnectionPoolMBean_Stub.getPassword()Ljava.lang.String;(JDBCConnectionPoolMBean_Stub.java:1449)
at weblogic.jdbc.common.internal.ConnectionPool.getDriverProperties()Ljava.util.Properties;(ConnectionPool.java:1479)
at weblogic.jdbc.common.internal.ConnectionPool.access$500(Lweblogic.jdbc.common.internal.ConnectionPool;)Ljava.util.Properties;(ConnectionPool.java:66)
at weblogic.jdbc.common.internal.ConnectionPool$3.run()Ljava.lang.Object;(ConnectionPool.java:1084)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(Lweblogic.security.subject.AbstractSubject;Ljava.security.PrivilegedExceptionAction;)Ljava.lang.Object;(AuthenticatedSubject.java:363)>>> at weblogic.security.service.SecurityManager.runAs(Lweblogic.security.acl.internal.AuthenticatedSubject;Lweblogic.security.acl.internal.AuthenticatedSubject;Ljava.security.PrivilegedExceptionAction;)Ljava.lang.Object;(SecurityManager.java:147)>>> at weblogic.jdbc.common.internal.ConnectionPool.initJDBCParameters()V(ConnectionPool.java:1080)>>> at weblogic.jdbc.common.internal.ConnectionPool.access$300(Lweblogic.jdbc.common.internal.ConnectionPool;)V(ConnectionPool.java:66)>>> at weblogic.jdbc.common.internal.ConnectionPool$1.run()Ljava.lang.Object;(ConnectionPool.java:1007)>>> at weblogic.security.acl.internal.AuthenticatedSubject.doAs(Lweblogic.security.subject.AbstractSubject;Ljava.security.PrivilegedExceptionAction;)Ljava.lang.Object;(AuthenticatedSubject.java:363)>>> at weblogic.security.service.SecurityManager.runAs(Lweblogic.security.acl.internal.AuthenticatedSubject;Lweblogic.security.acl.internal.AuthenticatedSubject;Ljava.security.PrivilegedExceptionAction;)Ljava.lang.Object;(SecurityManager.java:147)>>> at weblogic.jdbc.common.internal.ConnectionPool.doStart(Z)V(ConnectionPool.java:1004)>>> at weblogic.jdbc.common.internal.ConnectionPool.start(Ljava.lang.Object;)V(ConnectionPool.java:142)>>> at weblogic.jdbc.common.internal.ConnectionPoolManager.createAndStartPool(Lweblogic.management.configuration.DeploymentMBean;)V(ConnectionPoolManager.java:306)>>> at weblogic.jdbc.common.internal.JDBCService.addDeployment(Lweblogic.management.configuration.DeploymentMBean;)V(JDBCService.java:180)>>> at weblogic.management.mbeans.custom.DeploymentTarget.addDeployment(Lweblogic.management.configuration.DeploymentMBean;)Z(DeploymentTarget.java:337)>>> at weblogic.management.mbeans.custom.DeploymentTarget.addDeployment(Lweblogic.management.WebLogicObjectName;)Z(DeploymentTarget.java:202)>>> at jrockit.reflect.NativeMethodInvoker.invoke0(Ljava.lang.Object;ILjava.lang.Object;[Ljava.lang.Object;)Ljava.lang.Object;(Unknown Source)>>> at jrockit.reflect.NativeMethodInvoker.invoke(Ljava.lang.Object;[Ljava.lang.Object;)Ljava.lang.Object;(Unknown Source)>>> >>>> <Jun 27, 2006 3:51:31 PM EDT> <Error> <JDBC> <BEA-001150> <Connection Pool "cgPool" deployment failed with the following error: com.rsa.jsafe.JSAFE_PaddingException: Couldnot perform unpadding: invalid pad byte..>...</pre>Hi,
MailSession is a service. If you using WLST offline you have to use the <i>assign</i> command.
Check here http://e-docs.bea.com/wls/docs91/config_scripting/reference.html#1086620
Hope this helps
Ash. -
I'm reading book "JAVA THREAD" published by OREILLY.
And on fifth chapter, it gives an example.
one Thread's two method:
private boolean done = false;
public void run()
while(!done)
foo();
public void setDone()
done = true;
it says, the run method will be compiled as machine code:
Begin method run
load register r1 with memory location 0xff12345
Label L1:
Test if register r1 == 1
If true branch to L2
Call method foo
Branch to L1
Label L2:
End method run
setDone method will be compiled like:
Begin method setDone
Store 1 into memory location 0xff12345
End method setDone
And it says " because Run method will never reload 0xff12345 to register r1(in while loop), so setDone method will never lead to run stop.
I'm so puzzled with this. I have test this code on windows platform, run method can stop after another Thread call setDone method !.
but I think "JAVA THREAD" should have error on this, so why ?If the book says it will happen like that, then the book is wrong.
I think what they meant--and what would be correct to say--is that that is an example of what could happen if you don't synchronize all access to the run variable.
The point is this: Threads can have local copies of variables, that are separate from other threads' local copies and separate from the "master" copy. The spec doesn't define where those local copies live--the implementation can put them anywhere it wants--but the most natural and sensible thing would be to store the local copies in CPU registers, rather than in main mem.
The example the book gave shows what might happen if that VM stores threads' local copies in registers. There's no guarantee that the problem they described will happen, but it could, so you have to guard against it.
You guard against it by declaring that shared variable volatile, which requires that the threads use the master copy rather than their local copies, or by synchronizing every access to that thread. Syncing requires reading from the master copy on entering the sync block (or on first access) and writing out to the master copy upon leaving the sync block. -
The case:
I have a case where I hold configuration about a web page in an xml-file. The configuration is cached in memory on first request then I retrieve the configuration from memory on every following request. If the configuration in the xml-file changes between requests I will release the current configuration from memory and renew it by reading from the file.
Since we are talking about http requests all this will happen in a heavy loaded multithreaded environment.
The current solution:
+public class ConfigFile {+
private static final ConfigCache REGISTRY;
+static {+
REGISTRY = ModuleRegistry.create();
+}+
+public static Element getConfigElement(final String configKey) {+
Element configElm = null;
+if(configFileIsChanged()) {+
REGISTRY.clear();
+}+
+if((configElm = REGISTRY.get(configKey)) == null) {+
+... //Reload configuration from xml-file+
REGISTRY.put(configKey, configElm);//and save to registry
+}+
return configElm;//Return config data as an xml-element
+}+
+... //Class impl..+
+//This inner class holds the configuration as dom element parts+
+private static class ConfigCache {+
+private HashMap<String, Element> _moduleRegistry;+
+public synchronized void put(final String key, final Element elm) {+
_moduleRegistry.put(key, elm);+
+}+
+public synchronized Element get(final String module) {+
+return _moduleRegistry.get(key);+
+}+
+public synchronized void clear() {+
_moduleRegistry.clear();+
+}+
+public static ConfigCache create(){+
return new ConfigCache();
+}+
+}+
+}+
Since the registry is declared as a class variable in the ConfigFile class I handle the put and get operations to it by synchronizing the access.
The problem/question:
Is if this is enough to handle possible threading issues when it comes to invalidating the registry. What happens if some thread tries to "get" from the registry when another just cleared it? I guess it needs to wait until it's reloaded, right?
Regards
JohannesI wasn't clear enough in my previous post. Sorry for that.
The code will be executed in a servlet/JSP environment but it's not really relevant in this case except for the fact that there will be multiple threads accessing this code.
Think of it as an in memory data storage with information that will be read from the memory. The memory in this case is implemented as a static HashMap (the ConfigCache). I've wrapped it in the ConfigCache class since I want to synchronize the access to the put and get. All threads have access to the configuration file through the static method call ConfigFile.getModuleElement(somElementName);. This method is not synchronized.
Suppose we have 20 threads accessing this code concurrently. I'm not sure how the code will behave when this executes. My guess is that the critical parts are when one thread clears the HashMap, another tries to put something to the Map and another tries to get something.
I made an assertation test where I expected different results from 5 randomly executed web requests with 20 active threads reading and writing to the Map (the ConfigCache). It succeeds but I'm still not sure if I'm on the right track.
I've read a lot about this but I have not found any material describing this as clearly as I want it. In most cases they only explain how to use synchronized blocks and methods.
Are you with me or do you still think this belong in the servlet forum? Maybe it does?
BTW. How do I use code tags? -
Synchronization on a static object
If I have a code like:
synchronized(_otherParsers)
_otherParsers = new Vector();
} knowing the _otherParsers is a static object in the class, the synchronization will block all access to the object or it will block the access to the sync. block?
OBS: So when one thread enters the synchronized block, accesses to that object by other threads are blocked until the original thread exits the synchronized block. [PENDING: verify previous statement, and figure out if synch'ed blocks block out all method calls on the object or just synch'ed ones] (quote from Java Tutorial, http://java.sun.com/docs/books/tutorial/together/bingo/synchronized.html)IIRC, when using synchronize(someObject) then
someObject is locked, meaning that the original object
isn't locked. So other methods will execute correctly.let's say the following:import java.util.Vector;
public class A {
public static Vector v = null;
public A() {
v = new Vector();
public static void main(String[] args)
B b = new B();
new Thread(b).start();
new A();
class B implements Runnable {
public void run() {
synchronized(A.v) {
A.v = new Vector();
A.v.add("test");
}As you can clearly see, the static vector, member of A class is initialized twice (once by the new A() in the main method of A class and once by A.v = new Vector() in the run() method of B class). Assuming the thread enters the synchronized block before the call to new A() in the main method, when that call ocures, is the A.v object locked? or not? -
Strange PriorityQueue behaviour
Hey all,
Im observring somr rather peculiar behaviour with a PriorityQueue. Essentially I make a check that PriorityQueue pq has some elements in it i.e.
if(pq.size()>0){
// Then, I try and access (pop) the element at the head of the queue:
element = pq.poll();
...etc
However sometime, when it gets to pq.poll() in the above code, an exception is raised. NB, I has even tried if(pq.peek()!=null), yet occasionally, a null pointer exception is raised. Has anyone else noticed this very strange behaviour, and if so, what on earth could be hapenning?
Cheers
Ben.My understanding is that the threadsafe aspect of PriorityBlockingQueue ensures that the internal data will not be currupted when multiple threads are accessing it. This will not, however, prevent one thread from removing elements while another thread is checking peek() and doing poll().
I recommend synchronizing all access to pq so in between your peek() and your poll() another thread isn't adding/removnig elements out from under you.synchronized(pq) // one thread runs this
if(pq.peek() == null)
element = pq.poll();
element.method();
synchronized(pq) // another thread attempts to run this, but must wait until it's safe.
pq.add(...);
pq.remove(...);
} -
A game example in wireless toolkit: why synchronize?
For exmaple of wormgame come with j2me wireless toolkit,the worm is synchronized when accessed.
synchronized (worm) {
worm.removeAllElements();
worm.addElement(new WormLink(INIT_X, INIT_Y,
INIT_LEN, INIT_DIR));
// Reset class variables
currentDirection = INIT_DIR;
needUpdate = false;
hasEaten = false;
moveOnNextUpdate = false;
But when start the game MIDlet,only one thread is create.
in wormmain.java
protected void startApp() {
Display.getDisplay(this).setCurrent(theGame);
try {
// Start the game in its own thread
Thread myThread = new Thread(theGame);
myThread.start();
} catch (Error e) {
destroyApp(false);
notifyDestroyed();
Does it really need to synchronzied the worm?I know this message is a little old already, but I'll reply anyway.
I haven't examined the code, but when you create a new thread, you will not only have this thread running, but also the main thread. So you have actually two threads running. And if they both modify the worm Vector it is necessary to synchronize it.
Maybe you are looking for
-
Another: a serious error has occurred that requires Adobe Premiere Pro to shut down
I know a few people have had the same notification: sorry, a serious error has occurred that requires Adobe Premiere Pro to shut down. We will attempt to save your current project I have tried a whole series of things to fix it and no luck. Here are
-
How do I save files to my computer?
How do I save a file I created and uploaded to my computer?
-
How to get total of any field in sapscript?
Hello , i m making sapscript... How to get total of any field in sapscript?
-
Missing Parameters when JSP Forwarding
I am having difficulty with passing parameters when a forward occurs from one JSP to a second JSP. When I try to get those parameters on the second JSP I am getting nulls since it never actually receives them as parameters. Here is what the code look
-
hi friends.. can u tell me.. what is unicode check ? in which cases we use this? what is enhancement category ? in which cases we use this?